What I am trying to do here is that based on the parent task execution I am trying to execute or cancel the subsequent async task. I am finding it hard to wrap my head around the execution of the following and trying to implement this in the most optimal way. I am looking for a way to write this in the most optimized way.
Also, I am confused as to why even after throwing up an exception it continues with the next iteration and await the task to figure out the exception thrown. I Can't find an explanation around this.
public static async Task<TOut> AndThen<TIn, TOut>(this Task<TIn> sourceTask, Func<TIn, Task<TOut>> sf, CancellationToken cancelToken) { return await sourceTask.ContinueWith(async st => { var res = await st; // Raising cancel request in here. cancelToken.ThrowIfCancellationRequested(); return await sf(res); }, TaskContinuationOptions.NotOnFaulted & TaskContinuationOptions.NotOnCanceled).Unwrap(); }
TaskContinuationOptions.NotOnFaulted | TaskContinuationOptions.NotOnCanceledrather than&.sourceTaskcompletes in faulted state?