-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTaskExtensions.cs
504 lines (462 loc) · 23.8 KB
/
TaskExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
using System;
using System.Threading;
using System.Threading.Tasks;
namespace RLC.TaskChaining;
using static TaskStatics;
public static partial class TaskExtensions
{
#region Monadic Functions
private static Exception PotentiallyUnwindException(Exception exception) => exception is AggregateException aggregateException
? aggregateException.InnerException ?? exception
: exception;
private static Exception HandleCancellation<T>(this Task<T> task)
{
try
{
T result = task.Result;
return new Exception("Expected canceled task");
}
catch (OperationCanceledException exception)
{
return exception;
}
}
/// <summary>
/// Monadic 'alt'.
/// </summary>
/// <remarks>This method allows you to swap a faulted <see name="Task{T}"/> with an alternate
/// <see name="Task{T}"/>.</remarks>
/// <typeparam name="T">The task's underlying type.</typeparam>
/// <param name="task">The task.</param>
/// <param name="other">The replacement <see name="Task{T}"/>.</param>
/// <returns>The alternative value if the original <see name="Task{T}"/> was faulted, otherwise the original
/// <see name="Task{T}"/>.</returns>
public static Task<T> Alt<T>(this Task<T> task, Task<T> other) => task.IsFaulted ? other : task;
/// <summary>
/// Monadic 'alt'.
/// </summary>
/// <remarks>This method allows you to swap a faulted <see name="Task{T}"/> with an alternate <see name="Task{T}"/>
/// produced by the supplier function.</remarks>
/// <typeparam name="T">The task's underlying type.</typeparam>
/// <param name="task">The task.</param>
/// <param name="supplier">A supplier function.</param>
/// <returns>The alternative value if the original <see name="Task{T}"/> was faulted, otherwise the original
/// <see name="Task{T}"/>.</returns>
public static Task<T> Alt<T>(this Task<T> task, Func<Task<T>> supplier) => task.IsFaulted ? supplier() : task;
/// <summary>
/// Monadic 'ap'.
/// </summary>
/// <typeparam name="T">The task's underlying type.</typeparam>
/// <typeparam name="TR">The type of the other input value to <paramref name="morphismTask"/>.</typeparam>
/// <typeparam name="TNext">The transformed type.</typeparam>
/// <param name="task">The task.</param>
/// <param name="morphismTask">A <see cref="Task{Func{T, TR, TNext}}"/> containing the transformation function. </param>
/// <returns>The transformed task.</returns>
public static Task<TNext> Ap<T, TNext>(
this Task<T> task,
Task<Func<T, TNext>> morphismTask
) => morphismTask.IsFaulted
? Task.FromException<TNext>(PotentiallyUnwindException(morphismTask.Exception!))
: task.ResultMap(value => morphismTask.Result(value));
/// <summary>
/// Monadic 'bimap'.
/// </summary>
/// <remarks>This method allows you to specify a transformation for both sides (fulfilled and faulted) of a
/// <see name="Task{T}"/>. If the input <see name="Task{T}"/> is fulfilled, the <code>onFulfilled</code> function
/// will be executed, otherwise the <code>onFaulted</code> function will be executed. Note that this method
/// does NOT allow a faulted <see name="Task{T}"/> to be transitioned back into a fulfilled one.</remarks>
/// <typeparam name="T">The task's underlying type.</typeparam>
/// <typeparam name="TNext">The transformed type.</typeparam>
/// <param name="task">The task.</param>
/// <param name="onFaulted">The function to run if the task is faulted.</param>
/// <param name="onFulfilled">The function to run if the task is fulfilled.</param>
/// <returns>The transformed task.</returns>
public static Task<TNext> BiMap<T, TNext>(
this Task<T> task,
Func<Exception, Exception> onFaulted,
Func<T, TNext> onFulfilled
) => task.BiBind(
Pipe2(onFaulted, Task.FromException<TNext>),
Pipe2(onFulfilled, Task.FromResult)
);
/// <summary>
/// Monadic 'bind'.
/// </summary>
/// <remarks>This is the normal monadic 'bind' in which a function transforms the <see name="Task{T}"/>'s unwrapped
/// value into a wrapped <see name="Task{T}"/>.</remarks>
/// <typeparam name="T">The task's underlying type.</typeparam>
/// <typeparam name="TNext">The transformed type.</typeparam>
/// <param name="task">The task.</param>
/// <param name="onFulfilled">The function to run if the task is fulfilled.</param>
/// <returns>The transformed task.</returns>
public static Task<TNext> Bind<T, TNext>(this Task<T> task, Func<T, Task<TNext>> onFulfilled) => task.BiBind(
Task.FromException<TNext>,
onFulfilled
);
/// <summary>
/// Monadic 'bind' on both sides of a <see name="Task{T}"/>.
/// </summary>
/// <remarks>This method allows either side of a <see name="Task{T}"/> (faulted or fulfilled) to be bound to a new
/// type. Note that this method requires a faulted <see name="Task{T}"/> to be transitioned back into a fulfilled
/// one.</remarks>
/// <typeparam name="T">The task's underlying type.</typeparam>
/// <typeparam name="TNext">The transformed type.</typeparam>
/// <returns>The transformed task.</returns>
public static Task<TNext> BiBind<T, TNext>(
this Task<T> task,
Func<Exception, Task<TNext>> onFaulted,
Func<T, Task<TNext>> onFulfilled
) => task.ContinueWith(continuationTask =>
{
if (continuationTask.IsCanceled)
{
return Task.FromException<TNext>(HandleCancellation(task));
}
return continuationTask.IsFaulted
? onFaulted(PotentiallyUnwindException(continuationTask.Exception!))
: onFulfilled(continuationTask.Result);
}).Unwrap();
/// <summary>
/// Disjunctive `leftMap`.
/// </summary>
/// <remarks>This method allows a faulted <see name="Task{T}"/>'s <see name="Exception"/> to be transformed into
/// another type of <see name="Exception"/> (<see name="Task{T}"/>'s left side is pinned to <see name="Exception"/>).
/// This can be used to transform an <see name="Exception"/> to add context or to use a custom exception
/// type.</remarks>
/// <typeparam name="T">The task's underlying type.</typeparam>
/// <param name="task">The task.</param>
/// <param name="onFaulted">The transformation function.</param>
/// <returns>The transformated task.</returns>
public static Task<T> ExceptionMap<T>(this Task<T> task, Func<Exception, Exception> onFaulted)
=> task.BiMap(onFaulted, Identity);
public static Task<T> ExceptionMap<T>(this Task<T> task, Func<Exception, Task<Exception>> onFaulted)
=> task.ContinueWith(continuationTask => continuationTask.IsFaulted
? Task.FromException<T>(onFaulted(PotentiallyUnwindException(continuationTask.Exception)).Exception)
: continuationTask
).Unwrap();
/// <summary>
/// Allows a fulfilled <see name="Task{T}"/> to be transitioned to a faulted one if the <paramref name="predicate"/>
/// returns <code>false</code>.
/// </summary>
/// <remarks>This method provides another way to perform validation on the value contained within the
/// <see name="Task{T}"/>.</remarks>
/// <typeparam name="T">The task's underlying type.</typeparam>
/// <param name="task">The task.</param>
/// <param name="predicate">The predicate to run on the <see name="Task{T}"/>'s value.</param>
/// <param name="exception">The exception to fault with if the <paramref name="predicate"/> returns
/// <code>false</code>.</param>
/// <returns>The transformed task.</returns>
public static Task<T> Filter<T>(this Task<T> task, Predicate<T> predicate, Exception exception)
=> task.Filter(predicate, Constant(exception));
/// <summary>
/// Allows a fulfilled <see name="Task{T}"/> to be transitioned to a faulted one if the <paramref name="predicate"/>
/// returns <code>false</code>.
/// </summary>
/// <remarks>This method provides another way to perform validation on the value contained within the
/// <see name="Task{T}"/>.</remarks>
/// <typeparam name="T">The task's underlying type.</typeparam>
/// <param name="task">The task.</param>
/// <param name="predicate">The predicate to run on the <see name="Task{T}"/>'s value.</param>
/// <param name="supplier">A supplier function that produces the exception to fault with if the
/// <paramref name="predicate"/> returns <code>false</code>.</param>
/// <returns>The transformed task.</returns>
public static Task<T> Filter<T>(this Task<T> task, Predicate<T> predicate, Func<Exception> supplier)
=> task.Filter(predicate, _ => supplier());
/// <summary>
/// Allows a fulfilled <see cref="Task{T}"/> to be transitioned to a faulted one if the <paramref name="predicate"/>
/// returns <code>false</code>.
/// </summary>
/// <remarks>This method provides another way to perform validation on the value contained within the
/// <see name="Task{T}"/>.</remarks>
/// <typeparam name="T">The task's underlying type.</typeparam>
/// <param name="task">The task.</param>
/// <param name="predicate">The predicate to run on the <see cref="Task{T}"/>'s value.</param>
/// <param name="morphism">A function that produces the exception to fault with if the <paramref name="predicate"/>
/// returns <code>false</code>.</param>
/// <returns>The transformed task.</returns>
public static Task<T> Filter<T>(this Task<T> task, Predicate<T> predicate, Func<T, Exception> morphism)
=> task.Bind(value => predicate(value) ? Task.FromResult(value) : Task.FromException<T>(morphism(value)));
/// <summary>
/// Allows a fulfilled <see cref="Task{T}"/> to be transitioned to a faulted one if the <paramref name="predicate"/>
/// return <code>false</code>.
/// </summary>
/// <typeparam name="T">The task's underlying type.</typeparam>
/// <typeparam name="E">The type of <see cref="Exception"/> that <paramref name="morphism"/> returns.</typeparam>
/// <param name="task">The task.</param>
/// <param name="predicate">The predicate to run on the <see cref="Task{T}"/>'s value.</param>
/// <param name="morphism">A function that produces a <see cref="Task{E}"/> to fault with if the
/// <paramref name="predicate"/>returns <code>false</code>.</param>
/// <returns>The transformed task.</returns>
public static Task<T> Filter<T, E>(
this Task<T> task,
Predicate<T> predicate,
Func<Task<E>> morphism
) where E : Exception => task.Filter(predicate, _ => morphism());
/// <summary>
/// Allows a fulfilled <see cref="Task{T}"/> to be transitioned to a faulted one if the <paramref name="predicate"/>
/// return <code>false</code>.
/// </summary>
/// <typeparam name="T">The task's underlying type.</typeparam>
/// <typeparam name="E">The type of <see cref="Exception"/> that <paramref name="morphism"/> returns.</typeparam>
/// <param name="task">The task.</param>
/// <param name="predicate">The predicate to run on the <see cref="Task{T}"/>'s value.</param>
/// <param name="morphism">A function that produces a <see cref="Task{E}"/> to fault with if the
/// <paramref name="predicate"/>returns <code>false</code>.</param>
/// <returns>The transformed task.</returns>
public static Task<T> Filter<T, E>(
this Task<T> task,
Predicate<T> predicate,
Func<T, Task<E>> morphism
) where E: Exception
{
return task.Then(value => predicate(value)
? Task.FromResult(value)
: morphism(value).Then(Task.FromException<T>)
);
}
/// <summary>
/// Allows a fulfilled <see name="Task{T}"/> to be transitioned to a faulted one if the <paramref name="predicate"/>
/// returns <code>false</code>.
/// </summary>
/// <remarks>This method provides another way to perform validation on the value contained within the
/// <see name="Task{T}"/>.</remarks>
/// <typeparam name="T">The task's underlying type.</typeparam>
/// <param name="task">The task.</param>
/// <param name="predicate">The predicate to run on the <see name="Task{T}"/>'s value.</param>
/// <param name="exception">The exception to fault with if the <paramref name="predicate"/> returns
/// <code>false</code>.</param>
/// <returns>The transformed task.</returns>
public static Task<T> Filter<T>(this Task<T> task, Func<T, Task<bool>> predicate, Exception exception)
=> task.Filter(predicate, Constant(exception));
/// <summary>
/// Allows a fulfilled <see name="Task{T}"/> to be transitioned to a faulted one if the <paramref name="predicate"/>
/// returns <code>false</code>.
/// </summary>
/// <remarks>This method provides another way to perform validation on the value contained within the
/// <see name="Task{T}"/>.</remarks>
/// <typeparam name="T">The task's underlying type.</typeparam>
/// <param name="task">The task.</param>
/// <param name="predicate">The predicate to run on the <see name="Task{T}"/>'s value.</param>
/// <param name="supplier">A supplier function that produces the exception to fault with if the
/// <paramref name="predicate"/> returns <code>false</code>.</param>
/// <returns>The transformed task.</returns>
public static Task<T> Filter<T>(this Task<T> task, Func<T, Task<bool>> predicate, Func<Exception> supplier)
=> task.Filter(predicate, _ => supplier());
/// <summary>
/// Allows a fulfilled <see cref="Task{T}"/> to be transitioned to a faulted one if the <paramref name="predicate"/>
/// returns <code>false</code>.
/// </summary>
/// <remarks>This method provides another way to perform validation on the value contained within the
/// <see name="Task{T}"/>.</remarks>
/// <typeparam name="T">The task's underlying type.</typeparam>
/// <param name="task">The task.</param>
/// <param name="predicate">The predicate to run on the <see cref="Task{T}"/>'s value.</param>
/// <param name="morphism">A function that produces the exception to fault with if the <paramref name="predicate"/>
/// returns <code>false</code>.</param>
/// <returns>The transformed task.</returns>
public static Task<T> Filter<T>(
this Task<T> task,
Func<T, Task<bool>> predicate,
Func<T, Exception> morphism
)
{
return task.Then(value => predicate(value)
.Then(result => result
? Task.FromResult(value)
: Task.FromException<T>(morphism(value))
));
}
/// <summary>
/// Allows a fulfilled <see cref="Task{T}"/> to be transitioned to a faulted one if the <paramref name="predicate"/>
/// return <code>false</code>.
/// </summary>
/// <typeparam name="T">The task's underlying type.</typeparam>
/// <typeparam name="E">The type of <see cref="Exception"/> that <paramref name="morphism"/> returns.</typeparam>
/// <param name="task">The task.</param>
/// <param name="predicate">The predicate to run on the <see cref="Task{T}"/>'s value.</param>
/// <param name="morphism">A function that produces a <see cref="Task{E}"/> to fault with if the
/// <paramref name="predicate"/>returns <code>false</code>.</param>
/// <returns>The transformed task.</returns>
public static Task<T> Filter<T, E>(
this Task<T> task,
Func<T, Task<bool>> predicate,
Func<Task<E>> morphism
) where E : Exception
{
return task.Then(value => predicate(value)
.Then(result => result
? Task.FromResult(value)
: morphism().Then(Task.FromException<T>))
);
}
/// <summary>
/// Allows a fulfilled <see cref="Task{T}"/> to be transitioned to a faulted one if the <paramref name="predicate"/>
/// return <code>false</code>.
/// </summary>
/// <typeparam name="T">The task's underlying type.</typeparam>
/// <typeparam name="E">The type of <see cref="Exception"/> that <paramref name="morphism"/> returns.</typeparam>
/// <param name="task">The task.</param>
/// <param name="predicate">The predicate to run on the <see cref="Task{T}"/>'s value.</param>
/// <param name="morphism">A function that produces a <see cref="Task{E}"/> to fault with if the
/// <paramref name="predicate"/>returns <code>false</code>.</param>
/// <returns>The transformed task.</returns>
public static Task<T> Filter<T, E>(
this Task<T> task,
Func<T, Task<bool>> predicate,
Func<T, Task<E>> morphism
) where E : Exception
{
return task.Then(value => predicate(value)
.Then(result => result
? Task.FromResult(value)
: morphism(value).Then(Task.FromException<T>)
));
}
/// <summary>
/// Disjunctive 'rightMap'. Can be thought of as 'fmap' for <see name="Task{T}"/>s.
/// </summary>
/// <remarks>This method allows the value in a fulfilled <see name="Task{T}"/> to be transformed into another
/// type.</remarks>
/// <typeparam name="T">The task's underlying type.</typeparam>
/// <typeparam name="TNext">The transformed type.</typeparam>
/// <param name="task">The task.</param>
/// <param name="morphism">The transformation function.</param>
/// <returns>The transformed task.</returns>
public static Task<TNext> ResultMap<T, TNext>(this Task<T> task, Func<T, TNext> morphism)
=> task.BiMap(Identity, morphism);
/// <summary>
/// Allows a faulted <see name="Task{T}"/> to be transitioned to a fulfilled one.
/// </summary>
/// <typeparam name="T">The task's underlying type.</typeparam>
/// <param name="task">The task.</param>
/// <param name="morphism">The function to convert an <see name="Exception"/> into a <typeparamref name="T"/>.</param>
/// <returns>The transformed task.</returns>
public static Task<T> Recover<T>(this Task<T> task, Func<Exception, T> morphism)
=> task.Then(Identity, morphism);
/// <summary>
/// Allows a faulted <see name="Task{T}"/> to be transitioned to a fulfilled one.
/// </summary>
/// <typeparam name="T">The task's underlying type.</typeparam>
/// <param name="task">The task.</param>
/// <param name="morphism">The function to convert an <see name="Exception"/> into a <typeparamref name="T"/>.</param>
/// <returns>The transformed task.</returns>
public static Task<T> Recover<T>(this Task<T> task, Func<Exception, Task<T>> morphism)
=> task.BiBind(morphism, Task.FromResult);
#endregion
/// <summary>
/// Allows a faulted <see name="Task{T}"/> to be transitioned to a fulfilled one.
/// </summary>
/// <remarks>This method is an alias to <code>Task.Recover</code>.</remarks>
/// <typeparam name="T">The task's underlying type.</typeparam>
/// <param name="task">The task.</param>
/// <param name="onFaulted">The function to convert an <see name="Exception"/> into a
/// <typeparamref name="T"/>.</param>
/// <returns>The transformed task.</returns>
public static Task<T> Catch<T>(this Task<T> task, Func<Exception, T> onFaulted) => task.Recover(onFaulted);
/// <summary>
/// Allows a faulted <see name="Task{T}"/> to be transitioned to a fulfilled one.
/// </summary>
/// <remarks>This method is an alias to <code>Task.Recover</code> in order to line up with the expected Promise
/// API.</remarks>
/// <typeparam name="T">The task's underlying type.</typeparam>
/// <param name="task">The task.</param>
/// <param name="onFaulted">The function to convert an <see name="Exception"/> into a
/// <typeparamref name="T"/>.</param>
/// <returns>The transformed task.</returns>
public static Task<T> Catch<T>(this Task<T> task, Func<Exception, Task<T>> onFaulted) => task.Recover(onFaulted);
/// <summary>
/// Transitions a faulted <see name="Task{T}"/> into a fulfilled one if the contained exception matches the supplied
/// type.
/// </summary>
/// <remarks>This method is an alias to <code>Task.Recover</code> in order to line up with the expected Promise
/// API.</remarks>
/// <typeparam name="T">The task's underlying type.</typeparam>
/// <typeparam name="TException">The type of exception to catch.</typeparam>
/// <param name="task">The task.</param>
/// <param name="onFaulted">The function to convert a <typeparamref name="TException" /> into a
/// <typeparamref name="T"/>.</param>
/// <returns>The transformed task.</returns>
public static Task<T> CatchWhen<T, TException>(this Task<T> task, Func<TException, T> onFaulted)
where TException : Exception => task.Recover(
ex => ex is TException
? Task.FromResult(onFaulted((TException) ex))
: task
);
/// <summary>
/// Transitions a faulted <see name="Task{T}"/> into a fulfilled one if the contained exception matches the supplied
/// type.
/// </summary>
/// <remarks>This method is an alias to <code>Task.Recover</code> in order to line up with the expected Promise
/// API.</remarks>
/// <typeparam name="T">The task's underlying type.</typeparam>
/// <typeparam name="TException">The type of exception to catch.</typeparam>
/// <param name="task">The task.</param>
/// <param name="onFaulted">The function to convert a <typeparamref name="TException" /> into a
/// <typeparamref name="T"/>.</param>
/// <returns>The transformed task.</returns>
public static Task<T> CatchWhen<T, TException>(this Task<T> task, Func<TException, Task<T>> onFaulted)
where TException : Exception => task.Recover(
ex => ex is TException
? onFaulted((TException) ex)
: task
);
public static Task<T> Delay<T>(
this Task<T> task,
TimeSpan delayInterval,
CancellationToken cancellationToken = default
) => task.ContinueWith(continuationTask =>
{
return Task.Delay(delayInterval, cancellationToken)
.ContinueWith(delayTask => continuationTask.Result);
}).Unwrap();
/// <summary>
/// Faults a <see cref="Task{T}"/> with a provided <see cref="Exception"/>.
/// </summary>
/// <typeparam name="T">The task's underlying type.</typeparam>
/// <param name="task">The task.</param>
/// <param name="exception">The exception that will be used to fault the task.</param>
/// <returns>The task.</returns>
public static Task<T> Fault<T>(this Task<T> task, Exception exception) => task.Fault(_ => exception);
/// <summary>
/// Faults a <see cref="Task{T}"/> with an <see cref="Exception"/> produced by <paramref name="faultMorphism"/>.
/// </summary>
/// <typeparam name="T">The task's underlying type.</typeparam>
/// <param name="task">The task.</param>
/// <param name="faultMorphism">The function that will produce the <see cref="Exception"/>.</param>
/// <returns>The task.</returns>
public static Task<T> Fault<T>(this Task<T> task, Func<T, Exception> faultMorphism) => task.ResultMap(
value => Task.FromException<T>(faultMorphism(value))
).Unwrap();
/// <summary>
/// Faults a <see cref="Task{T}"/> with a provided <see cref="Exception"/>.
/// </summary>
/// <typeparam name="T">The input task's underlying type.</typeparam>
/// <typeparam name="TNext">The output task's underlying type.</typeparam>
/// <param name="task">The task.</param>
/// <param name="exception">The exception that will be used to fault the task.</param>
/// <returns>The task.</returns>
public static Task<TNext> Fault<T, TNext>(this Task<T> task, Exception exception) =>
task.Fault<T, TNext>(_ => exception);
/// <summary>
/// Faults a <see cref="Task{T}"/> with an <see cref="Exception"/> produced by <paramref name="faultMorphism"/>.
/// </summary>
/// <typeparam name="T">The input task's underlying type.</typeparam>
/// <typeparam name="TNext">The output task's underlying type.</typeparam>
/// <param name="task">The task.</param>
/// <param name="faultMorphism">The function that will produce the <see cref="Exception"/>.</param>
/// <returns>The task.</returns>
public static Task<TNext> Fault<T, TNext>(this Task<T> task, Func<T, Exception> faultMorphism) => task.ResultMap(
value => Task.FromException<TNext>(faultMorphism(value))
).Unwrap();
public static Task<TNext> Retry<T, TNext>(
this Task<T> task,
Func<T, Task<TNext>> retryFunc,
RetryParams retryOptions
) => task.ResultMap(value => TaskExtras.Retry(() => retryFunc(value), retryOptions)).Unwrap();
public static Task<TNext> Retry<T, TNext>(this Task<T> task, Func<T, TNext> retryFunc, RetryParams retryOptions)
=> task.Retry(value => Task.FromResult(retryFunc(value)), retryOptions);
public static Task<TNext> Retry<T, TNext>(this Task<T> task, Func<T, Task<TNext>> retryFunc)
=> task.Retry(retryFunc, RetryParams.Default);
public static Task<TNext> Retry<T, TNext>(this Task<T> task, Func<T, TNext> retryFunc)
=> task.Retry(retryFunc, RetryParams.Default);
}