2
votes

I've written the following extension method on tasks

public static async Task<TOut> TaskMap<TIn,TOut>(this Task<TIn> task, Func<TIn,TOut> f)
{
   TIn x = await task;
   return f(x);
}

I'm hoping what this does is just create a new task like the original one which just applies f after the original task completes.

That is, Task y = t.TaskMap(x => x) should really be the same as Task y = t, barring a quick object construction and function call. It shouldn't fundamentally change when the original task is executed.

My questions are:

  1. Is this function sensible and does what I intend?
  2. Does it already exist?
Related GitHub issue: API Proposal: Then or ContinueWithResult extension method for Task. Microsoft is not very warm at including it in the standard libraries.Theodor Zoulias