Skip to content

Commit

Permalink
Merge pull request #56 from doomchild/dev
Browse files Browse the repository at this point in the history
2.14.0 for real this time
  • Loading branch information
doomchild authored May 14, 2023
2 parents 544e0bb + 88bc8ae commit 55ca2ce
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 29 deletions.
2 changes: 1 addition & 1 deletion project-metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
{
Expand Down
60 changes: 32 additions & 28 deletions src/TaskExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,6 @@ public static Task<T> ExceptionMap<T>(this Task<T> task, Func<Exception, Task<Ex
? Task.FromException<T>(await onFaulted(PotentiallyUnwindException(continuationTask.Exception)))
: continuationTask
).Unwrap().Unwrap();
// => task.BiBind<T, T>(
// //Pipe2<Exception, Task<Exception>, Task<T>>(onFaulted, Task.FromException<T>),
// async ex => Task.FromException<T>(await onFaulted(ex)),
// Pipe2(Identity, Task.FromResult)
// );

/// <summary>
/// Allows a fulfilled <see name="Task{T}"/> to be transitioned to a faulted one if the <paramref name="predicate"/>
Expand Down Expand Up @@ -369,16 +364,19 @@ public static Task<T> IfFulfilled<T>(this Task<T> task, Action<T> consumer)
/// <param name="func">The function to execute if the task is fulfilled.</param>
/// <returns>The task.</returns>
public static Task<T> IfFulfilled<T, R>(this Task<T> task, Func<T, Task<R>> 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();

/// <summary>
/// Executes a function and throws away the result if the <see name="Task{T}"/> is in a fulfilled state.
Expand All @@ -389,7 +387,7 @@ public static Task<T> IfFulfilled<T, R>(this Task<T> task, Func<T, Task<R>> func
/// <param name="func">The function to execute if the task is fulfilled.</param>
/// <returns>The task.</returns>
public static Task<T> IfFulfilled<T>(this Task<T> task, Func<T, Task> func)
=> task.ResultMap(TaskStatics.Tap(func)).Unwrap();
=> task.IfFulfilled<T, T>(value => Task.FromResult(value).Then(func).Then(_ => value, _ => value));

/// <summary>
/// Performs an action if the <see name="Task{T}"/> is in a faulted state.
Expand All @@ -404,31 +402,37 @@ public static Task<T> IfFaulted<T>(this Task<T> task, Action<Exception> onFaulte
/// Executes a function and throws away the result if the <see name="Task{T}"/> is in a faulted state.
/// </summary>
/// <typeparam name="T">The task's underlying type.</typeparam>
/// <typeparam name="R">The output task's underlying type.</typeparam>
/// <param name="onFaulted">The function to execute if the task is faulted.</param>
/// <returns>The task.</returns>
public static Task<T> IfFaulted<T, R>(this Task<T> task, Func<Exception, Task<R>> onFaulted)
=> task.ExceptionMap(async exception =>
=> task.ContinueWith(continuationTask =>
{
try
if (continuationTask.IsFaulted)
{
await onFaulted(exception);
Exception taskException = PotentiallyUnwindException(continuationTask.Exception!);
return Task.FromException<R>(PotentiallyUnwindException(continuationTask.Exception!))
.Catch<R>(ex => onFaulted(ex))
.Then(
_ => Task.FromException<T>(taskException),
_ => Task.FromException<T>(taskException)
);
}
catch { }
return exception;
});

public static Task<T> IfFaulted<T>(this Task<T> task, Func<Exception, Task> onFaulted)
=> task.ExceptionMap(async exception =>
{
try
else
{
await onFaulted(exception);
return continuationTask;
}
catch { }
}).Unwrap();

return exception;
});
/// <summary>
/// Executes a function and throws away the result if the <see name="Task{T}"/> is in a faulted state.
/// </summary>
/// <typeparam name="T">The task's underlying type.</typeparam>
/// <param name="onFaulted">The function to execute if the task is faulted.</param>
/// <returns>The task.</returns>
public static Task<T> IfFaulted<T>(this Task<T> task, Func<Exception, Task> onFaulted)
=> task.IfFaulted<T, T>(exception => onFaulted(exception).ContinueWith(continuationTask => Task.FromException<T>(exception)).Unwrap());

public static Task<TNext> Retry<T, TNext>(
this Task<T> task,
Expand Down

0 comments on commit 55ca2ce

Please sign in to comment.