From 58e8e29f16d7b3240f04dfa8c32df0c803cba3b1 Mon Sep 17 00:00:00 2001 From: doomchild Date: Sun, 14 May 2023 12:53:49 -0500 Subject: [PATCH 1/2] Changed IfFulfilled/IfFaulted to chain instead of awaiting --- src/TaskExtensions.cs | 60 +++++++++++++++++++++++-------------------- 1 file changed, 32 insertions(+), 28 deletions(-) diff --git a/src/TaskExtensions.cs b/src/TaskExtensions.cs index 460ad28..d19d106 100644 --- a/src/TaskExtensions.cs +++ b/src/TaskExtensions.cs @@ -122,11 +122,6 @@ public static Task ExceptionMap(this Task task, Func(await onFaulted(PotentiallyUnwindException(continuationTask.Exception))) : continuationTask ).Unwrap().Unwrap(); - // => task.BiBind( - // //Pipe2, Task>(onFaulted, Task.FromException), - // async ex => Task.FromException(await onFaulted(ex)), - // Pipe2(Identity, Task.FromResult) - // ); /// /// Allows a fulfilled to be transitioned to a faulted one if the @@ -369,16 +364,19 @@ public static Task IfFulfilled(this Task task, Action consumer) /// The function to execute if the task is fulfilled. /// The task. public static Task IfFulfilled(this Task task, Func> func) - => task.ResultMap(async value => + => task.ContinueWith(async continuationTask => { - try + if (continuationTask.IsFaulted) { - await func(value); + return continuationTask; } - catch { } + else + { + T value = await continuationTask; - return value; - }).Unwrap(); + return Task.FromResult(value).Then(func).Then(_ => value, _ => value); + } + }).Unwrap().Unwrap(); /// /// Executes a function and throws away the result if the is in a fulfilled state. @@ -389,7 +387,7 @@ public static Task IfFulfilled(this Task task, Func> func /// The function to execute if the task is fulfilled. /// The task. public static Task IfFulfilled(this Task task, Func func) - => task.ResultMap(TaskStatics.Tap(func)).Unwrap(); + => task.IfFulfilled(value => Task.FromResult(value).Then(func).Then(_ => value, _ => value)); /// /// Performs an action if the is in a faulted state. @@ -404,31 +402,37 @@ public static Task IfFaulted(this Task task, Action onFaulte /// Executes a function and throws away the result if the is in a faulted state. /// /// The task's underlying type. + /// The output task's underlying type. /// The function to execute if the task is faulted. /// The task. public static Task IfFaulted(this Task task, Func> onFaulted) - => task.ExceptionMap(async exception => + => task.ContinueWith(continuationTask => { - try + if (continuationTask.IsFaulted) { - await onFaulted(exception); + Exception taskException = PotentiallyUnwindException(continuationTask.Exception!); + + return Task.FromException(PotentiallyUnwindException(continuationTask.Exception!)) + .Catch(ex => onFaulted(ex)) + .Then( + _ => Task.FromException(taskException), + _ => Task.FromException(taskException) + ); } - catch { } - - return exception; - }); - - public static Task IfFaulted(this Task task, Func onFaulted) - => task.ExceptionMap(async exception => - { - try + else { - await onFaulted(exception); + return continuationTask; } - catch { } + }).Unwrap(); - return exception; - }); + /// + /// Executes a function and throws away the result if the is in a faulted state. + /// + /// The task's underlying type. + /// The function to execute if the task is faulted. + /// The task. + public static Task IfFaulted(this Task task, Func onFaulted) + => task.IfFaulted(exception => onFaulted(exception).ContinueWith(continuationTask => Task.FromException(exception)).Unwrap()); public static Task Retry( this Task task, From 88bc8ae047b65148b931d63ef1efad387e653413 Mon Sep 17 00:00:00 2001 From: doomchild Date: Sun, 14 May 2023 13:20:38 -0500 Subject: [PATCH 2/2] Updated package version to 2.14.0 --- project-metadata.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project-metadata.json b/project-metadata.json index 31b6e86..de929d6 100644 --- a/project-metadata.json +++ b/project-metadata.json @@ -2,7 +2,7 @@ "name": "task-chaining", "description": "Extension methods to System.Threading.Task to allow Promise-like chaining", "title": "TaskChaining", - "version": "2.13.0", + "version": "2.14.0", "ciEnvironment": { "variables": [ {