C# transform an async task from one type to another

I'm used to working with the Scala programming language - using Scala I could map over futures, such as:

val response: Future[HttpResponse] = asyncHttpClient.GetRequest("www.google.com")

val statusCode: Future[Int] = response.map(r => r.statusCode)

Recently I've picked up working with C#, and I saw myself being in the same situation as the example above, however I couldn't figure out how "map" a task.

Here is an example of what I want to achieve:

Task<HttpResponseMessage> response = httpClient.GetAsync("www.google.com")

Task<int> statusCode = response.Map(response => response.StatusCode)

Thanks

Jon Skeet
people
quotationmark

I'm slightly surprised there isn't anything in the framework for this, to be honest. (More likely, there is an I haven't seen it.) You can build it fairly easily though:

public static async Task<TResult> Map<TSource, TResult>
    (Task<TSource> task, Func<TSource, TResult> selector)
    => selector(await task);

people

See more on this question at Stackoverflow