I recently learned about Task.ContinueWith and am trying to apply it to some existing code I have for better performance. I have something like this:
public static async Task<TResult> DoWork<T, TResult>(T value)
{
var fooResult = await Foo(value);
return await Bar<TResult>(fooResult);
}
private static Task<MyClass> Foo<T>(T value)
{
return GetMyClassFromValueAsync(value);
}
private static Task<TResult> Bar<TResult>(MyClass myClass)
{
return myClass.DoOperationAsync();
}
and was thinking I could improve it by doing something like this instead:
public static Task<TResult> DoWork<T, TResult>(T value)
{
return Foo(value).ContinueWith(fooResult => Bar<TResult>(fooResult.Result));
}
The problem that I'm running into is that ContinueWith returns a type of Task<Task<TResult>>
in this case because it wraps my async call in another task. The only solution I've found is to do something like this:
public static async Task<TResult> DoWork<T, TResult>(T value)
{
return await await Foo(value).ContinueWith(fooResult => Bar<TResult>(fooResult.Result));
}
but that just looks wrong and forces me to await it. Is there a better way to handle this situation?
There's also a lot happening behind the scenes with these calls, so if I'm taking the wrong approach or my intuition is leading me astray and I should stick with my original approach, please let me know why so I can learn and improve. Thanks!