by
12
8
2,810
1
Top 1% !
Popular
Famous
Nice
Easy-to-find
Specified
Refined
MultiPlatform
Popularity: 5181st place
ClassNameExtensions
InputName0task
InputName1onError
InputType0Task<T>
InputType1Func<TError,T>
Languagec#
LicenseMIT_X11
MethodNameCatch
OutputTypeTask<T>

C#  Task extensions for yer pleasure

helpful for c# TAP stuff....    ENJOI!
Copy Embed Code
<iframe id="embedFrame" style="width:600px; height:300px;"
src="https://www.snip2code.com/Embed/531383/C---Task-extensions-for-yer-pleasure?startLine=0"></iframe>
Click on the embed code to copy it into your clipboard Width Height
Leave empty to retrieve all the content Start End
// Task extensions public static class Extensions { public static Task<T> Catch<T,TError> (this Task<T> task, Func<TError,T> onError) where TError : Exception { var tcs = new TaskCompletionSource<T> (); task.ContinueWith (ant => { if (task.IsFaulted && task.Exception.InnerException is TError) { tcs.SetResult (onError ((TError) task.Exception.InnerException)); } else if (ant.IsCanceled) { tcs.SetCanceled (); } else if (task.IsFaulted) { tcs.SetException (ant.Exception.InnerException); } else { tcs.SetResult (ant.Result); } }); return tcs.Task; } /* public static Task<T> Catch<T,TError> (this Task<T> task, T onError) where TError : Exception { return task.Catch<T,TError> (ex => onError); } public static Task<T> Catch<T,TError> (this Task task, T onError) where TError : Exception { return task.ToTaskOfT<T>().Catch<T,TError> (onError); } public static Task Catch (this Task task) { return task.Catch<object,Exception> (null); } public static Task<T> Catch<T> (this Task<T> task, T valueIfError = default(T)) { return task.Catch<T,Exception> (valueIfError); } */ public async static Task<T> ToTaskOfT<T> (this Task t) { await t; return default(T); } public static async Task Then(this Task task, Func<Task> continuation) { await task; await continuation(); } public static async Task<TNewResult> Then<TNewResult>(this Task task, Func<Task<TNewResult>> continuation) { await task; return await continuation(); } public static async Task Then<TResult>(this Task<TResult> task, Func<TResult,Task> continuation) { await continuation(await task); } public static async Task<TNewResult> Then<TResult, TNewResult>(this Task<TResult> task, Func<TResult, Task<TNewResult>> continuation) { return await continuation(await task); } }
If you want to be updated about similar snippets, Sign in and follow our Channels

blog comments powered by Disqus