@@ -10,15 +10,15 @@ Cancels a task, meaning it will never complete.
10
10
{% tab title="Usage" %}
11
11
12
12
``` typescript
13
- const task = Task .of (5 ).cancel ();
13
+ const task = Task .of (5 ).cancel ()
14
14
```
15
15
16
16
{% endtab %}
17
17
18
18
{% tab title="Type Definition" %}
19
19
20
20
``` typescript
21
- type cancel = () => void ;
21
+ type cancel = () => void
22
22
` ` `
23
23
24
24
{% endtab %}
@@ -32,15 +32,15 @@ Given a task, when it has _succeeded_, pass the value through a mapping function
32
32
{% tab title="Usage" %}
33
33
34
34
` ` ` typescript
35
- const task: Task <unknown , number > = Task .of (5 ).map (number => number * 2 );
35
+ const task: Task <unknown , number > = Task .of (5 ).map (number => number * 2 )
36
36
```
37
37
38
38
{% endtab %}
39
39
40
40
{% tab title="Type Definition" %}
41
41
42
42
``` typescript
43
- type map = <S2 >(fn : (result : S ) => S2 ) => Task <E , S2 >;
43
+ type map = <S2 >(fn : (result : S ) => S2 ) => Task <E , S2 >
44
44
` ` `
45
45
46
46
{% endtab %}
@@ -54,15 +54,39 @@ Given a task, when it has _failed_, pass the error through a mapping function to
54
54
{% tab title="Usage" %}
55
55
56
56
` ` ` typescript
57
- const task: Task <unknown , number > = Task .fail (5 ).mapError (number => number * 2 );
57
+ const task: Task <unknown , number > = Task .fail (5 ).mapError (number => number * 2 )
58
58
```
59
59
60
60
{% endtab %}
61
61
62
62
{% tab title="Type Definition" %}
63
63
64
64
``` typescript
65
- type mapError = <E2 >(fn : (error : E ) => E2 ) => Task <E2 , S >;
65
+ type mapError = <E2 >(fn : (error : E ) => E2 ) => Task <E2 , S >
66
+ ` ` `
67
+
68
+ {% endtab %}
69
+ {% endtabs %}
70
+
71
+ ## errorUnion
72
+
73
+ Expand the error types of a task. Purely a TypeScript type system modification.
74
+
75
+ {% tabs %}
76
+ {% tab title="Usage" %}
77
+
78
+ ` ` ` typescript
79
+ const task: Task <unknown | AnotherErrorPossibility , number > = Task .fail (
80
+ 5
81
+ ).errorUnion <AnotherErrorPossibility >()
82
+ ```
83
+
84
+ {% endtab %}
85
+
86
+ {% tab title="Type Definition" %}
87
+
88
+ ``` typescript
89
+ type errorUnion = <E2 >() => Task <E | E2 , S >
66
90
` ` `
67
91
68
92
{% endtab %}
@@ -79,7 +103,7 @@ Given a task, provide mapping functions for both the success and fail states. Re
79
103
const task: Task <Error , number > = Task .of (5 ).mapBoth (
80
104
() => new Error (" Surprising Error" ),
81
105
number => number * 2
82
- );
106
+ )
83
107
```
84
108
85
109
{% endtab %}
@@ -90,7 +114,7 @@ const task: Task<Error, number> = Task.of(5).mapBoth(
90
114
type mapBoth = <E2 , S2 >(
91
115
handleError : (error : E ) => E2 ,
92
116
handleSuccess : (success : S ) => S2
93
- ) => Task <E2 , S2 >;
117
+ ) => Task <E2 , S2 >
94
118
` ` `
95
119
96
120
{% endtab %}
@@ -108,15 +132,15 @@ In general, users have difficulty understanding the difference between `chain` a
108
132
` ` ` typescript
109
133
const task: Task <unknown , number > = Task .of (5 ).chain (number =>
110
134
Task .of (number * 2 )
111
- );
135
+ )
112
136
```
113
137
114
138
{% endtab %}
115
139
116
140
{% tab title="Type Definition" %}
117
141
118
142
``` typescript
119
- type chain = <S2 >(fn : (result : S ) => Task <E , S2 > | Promise <S2 >) => Task <E , S2 >;
143
+ type chain = <S2 >(fn : (result : S ) => Task <E , S2 > | Promise <S2 >) => Task <E , S2 >
120
144
` ` `
121
145
122
146
{% endtab %}
@@ -130,15 +154,15 @@ Given a task, wait some number of milliseconds to forward the successful value.
130
154
{% tab title="Usage" %}
131
155
132
156
` ` ` typescript
133
- const task: Task <unknown , number > = Task .of (5 ).wait (2000 );
157
+ const task: Task <unknown , number > = Task .of (5 ).wait (2000 )
134
158
```
135
159
136
160
{% endtab %}
137
161
138
162
{% tab title="Type Definition" %}
139
163
140
164
``` typescript
141
- type wait = (ms : number ) => Task <E , S >;
165
+ type wait = (ms : number ) => Task <E , S >
142
166
` ` `
143
167
144
168
{% endtab %}
@@ -154,15 +178,15 @@ Given a failing task, wait some number of seconds and attempt to retry it. Usefu
154
178
` ` ` typescript
155
179
const task: Task <unknown , Response > = Task .fromLazyPromise (() =>
156
180
fetch (URL )
157
- ).retryIn (2000 );
181
+ ).retryIn (2000 )
158
182
```
159
183
160
184
{% endtab %}
161
185
162
186
{% tab title="Type Definition" %}
163
187
164
188
``` typescript
165
- type retryIn = (ms : number ) => Task <E , S >;
189
+ type retryIn = (ms : number ) => Task <E , S >
166
190
` ` `
167
191
168
192
{% endtab %}
@@ -178,15 +202,15 @@ Given a task, continue to retry it some number of times. The time between each a
178
202
` ` ` typescript
179
203
const task: Task <unknown , Response > = Task .fromLazyPromise (() =>
180
204
fetch (URL )
181
- ).retryWithExponentialBackoff (2000 , 5 );
205
+ ).retryWithExponentialBackoff (2000 , 5 )
182
206
```
183
207
184
208
{% endtab %}
185
209
186
210
{% tab title="Type Definition" %}
187
211
188
212
``` typescript
189
- type retryWithExponentialBackoff = (ms : number , times : number ) => Task <E , S >;
213
+ type retryWithExponentialBackoff = (ms : number , times : number ) => Task <E , S >
190
214
` ` `
191
215
192
216
{% endtab %}
@@ -200,15 +224,15 @@ Given a task which succeeds with another task, flatten into a single task which
200
224
{% tab title="Usage" %}
201
225
202
226
` ` ` typescript
203
- const task: Task <unknown , number > = Task .of (Task .of (5 )).flatten ();
227
+ const task: Task <unknown , number > = Task .of (Task .of (5 )).flatten ()
204
228
```
205
229
206
230
{% endtab %}
207
231
208
232
{% tab title="Type Definition" %}
209
233
210
234
``` typescript
211
- type flatten = <S2 >(this : Task <E , Task <E , S2 >>) => Task <E , S2 >;
235
+ type flatten = <S2 >(this : Task <E , Task <E , S2 >>) => Task <E , S2 >
212
236
` ` `
213
237
214
238
{% endtab %}
@@ -224,7 +248,7 @@ Given a task that fails, a function can be called to attempt a recovery. This is
224
248
` ` ` typescript
225
249
const task: Task <string , string > = Task .fail (" Error" ).orElse (() =>
226
250
Task .of (" Success" )
227
- );
251
+ )
228
252
```
229
253
230
254
{% endtab %}
@@ -234,7 +258,7 @@ const task: Task<string, string> = Task.fail("Error").orElse(() =>
234
258
``` typescript
235
259
type orElse = <S2 >(
236
260
fn : (error : E ) => Task <E , S | S2 > | Promise <S | S2 >
237
- ) => Task <E , S | S2 >;
261
+ ) => Task <E , S | S2 >
238
262
` ` `
239
263
240
264
{% endtab %}
@@ -251,7 +275,7 @@ Given a task, provide a function to convert each of the success or error states
251
275
const task: Task <unknown , JSX > = Task .fromPromise (fetch (URL )).fold (
252
276
() => <h1 >Error < / h1 > ,
253
277
data => <h1 >Worked : $ {data }< / h1 >
254
- );
278
+ )
255
279
```
256
280
257
281
{% endtab %}
@@ -262,7 +286,7 @@ const task: Task<unknown, JSX> = Task.fromPromise(fetch(URL)).fold(
262
286
type fold = <R >(
263
287
handleError : (error : E ) => R ,
264
288
handleSuccess : (success : S ) => R
265
- ) => Task <unknown , R >;
289
+ ) => Task <unknown , R >
266
290
` ` `
267
291
268
292
{% endtab %}
@@ -278,15 +302,15 @@ Given a task, pass the success value to the tap (like tapping a tree or process)
278
302
` ` ` typescript
279
303
const task: Task <unknown , number > = Task .of (5 ).tap (num =>
280
304
console .log (" Got" , num )
281
- );
305
+ )
282
306
```
283
307
284
308
{% endtab %}
285
309
286
310
{% tab title="Type Definition" %}
287
311
288
312
``` typescript
289
- type tap = (fn : (result : S ) => void ) => Task <E , S >;
313
+ type tap = (fn : (result : S ) => void ) => Task <E , S >
290
314
` ` `
291
315
292
316
{% endtab %}
@@ -302,17 +326,15 @@ Given a task, pass the success value to the tap (like tapping a tree or process)
302
326
` ` ` typescript
303
327
const task: Task <unknown , number > = Task .of (5 ).tap (num =>
304
328
console .log (" Got" , num )
305
- );
329
+ )
306
330
```
307
331
308
332
{% endtab %}
309
333
310
334
{% tab title="Type Definition" %}
311
335
312
336
``` typescript
313
- type tapChain = <S2 >(
314
- fn : (result : S ) => Task <E , S2 > | Promise <S2 >
315
- ) => Task <E , S >;
337
+ type tapChain = <S2 >(fn : (result : S ) => Task <E , S2 > | Promise <S2 >) => Task <E , S >
316
338
` ` `
317
339
318
340
{% endtab %}
@@ -344,7 +366,7 @@ for (let i = 0; i < 5, i++) {
344
366
{% tab title="Type Definition" %}
345
367
346
368
``` typescript
347
- type onlyOnce = () => Task <E , S >;
369
+ type onlyOnce = () => Task <E , S >
348
370
` ` `
349
371
350
372
{% endtab %}
@@ -359,15 +381,15 @@ Like `onlyOnce`, but provides a function which can arbitrarily check whether the
359
381
360
382
` ` ` typescript
361
383
// Succeed until some unknown date.
362
- const task: Task <unknown , number > = Task .of (5 ).succeedIf (() => Date .now () < x );
384
+ const task: Task <unknown , number > = Task .of (5 ).succeedIf (() => Date .now () < x )
363
385
```
364
386
365
387
{% endtab %}
366
388
367
389
{% tab title="Type Definition" %}
368
390
369
391
``` typescript
370
- type succeedIf = (fn : () => S | undefined ) => Task <E , S >;
392
+ type succeedIf = (fn : () => S | undefined ) => Task <E , S >
371
393
` ` `
372
394
373
395
{% endtab %}
@@ -381,15 +403,15 @@ Converts a task to a Promise. Useful for integrating with other libraries.
381
403
{% tab title="Usage" %}
382
404
383
405
` ` ` typescript
384
- const promise: Promise <number > = Task .of (5 ).toPromise ();
406
+ const promise: Promise <number > = Task .of (5 ).toPromise ()
385
407
```
386
408
387
409
{% endtab %}
388
410
389
411
{% tab title="Type Definition" %}
390
412
391
413
``` typescript
392
- type toPromise = () => Promise <S >;
414
+ type toPromise = () => Promise <S >
393
415
` ` `
394
416
395
417
{% endtab %}
@@ -403,16 +425,16 @@ Swaps the error and success values so the old error message in the new success a
403
425
{% tab title="Usage" %}
404
426
405
427
` ` ` typescript
406
- const task: Task <unknown , number > = Task .of (5 );
407
- const swapped: Task <number , unknown > = task .swap ();
428
+ const task: Task <unknown , number > = Task .of (5 )
429
+ const swapped: Task <number , unknown > = task .swap ()
408
430
```
409
431
410
432
{% endtab %}
411
433
412
434
{% tab title="Type Definition" %}
413
435
414
436
``` typescript
415
- type swap = () => Task <S , E >;
437
+ type swap = () => Task <S , E >
416
438
` ` `
417
439
418
440
{% endtab %}
@@ -426,15 +448,15 @@ Given a successful Task, throw away the result and continue the chain with a new
426
448
{% tab title="Usage" %}
427
449
428
450
` ` ` typescript
429
- const task: Task <unknown , string > = Task .of (5 ).forward (() => " Hello" );
451
+ const task: Task <unknown , string > = Task .of (5 ).forward (() => " Hello" )
430
452
```
431
453
432
454
{% endtab %}
433
455
434
456
{% tab title="Type Definition" %}
435
457
436
458
``` typescript
437
- type forward = <S2 >(value : S2 ) => Task <E , S2 >;
459
+ type forward = <S2 >(value : S2 ) => Task <E , S2 >
438
460
` ` `
439
461
440
462
{% endtab %}
@@ -448,15 +470,15 @@ Given a successful Task, join it before an additional value. Useful for threadin
448
470
{% tab title="Usage" %}
449
471
450
472
` ` ` typescript
451
- const task: Task <unknown , [number , number ]> = Task .of (5 ).append (10 );
473
+ const task: Task <unknown , [number , number ]> = Task .of (5 ).append (10 )
452
474
```
453
475
454
476
{% endtab %}
455
477
456
478
{% tab title="Type Definition" %}
457
479
458
480
``` typescript
459
- type append = <S2 >(this : Task <E , S >, value : S2 ) => Task <E , [S , S2 ]>;
481
+ type append = <S2 >(this : Task <E , S >, value : S2 ) => Task <E , [S , S2 ]>
460
482
` ` `
461
483
462
484
{% endtab %}
@@ -470,15 +492,15 @@ Given a successful Task, join it after an additional value. Useful for threading
470
492
{% tab title="Usage" %}
471
493
472
494
` ` ` typescript
473
- const task: Task <unknown , [number , number ]> = Task .of (5 ).prepend (10 );
495
+ const task: Task <unknown , [number , number ]> = Task .of (5 ).prepend (10 )
474
496
```
475
497
476
498
{% endtab %}
477
499
478
500
{% tab title="Type Definition" %}
479
501
480
502
``` typescript
481
- type prepend = <S2 >(this : Task <E , S >, value : S2 ) => Task <E , [S2 , S ]>;
503
+ type prepend = <S2 >(this : Task <E , S >, value : S2 ) => Task <E , [S2 , S ]>
482
504
` ` `
483
505
484
506
{% endtab %}
@@ -499,7 +521,7 @@ Also allows the definition of the mapping function to be asychronous because it
499
521
const task: Task <unknown , number > = Task .of ((a , b , c ) => a + b + c )
500
522
.ap (succeed (10 )) // a
501
523
.ap (succeed (50 )) // b
502
- .ap (succeed (100 )); // c
524
+ .ap (succeed (100 )) // c
503
525
```
504
526
505
527
{% endtab %}
@@ -509,7 +531,7 @@ const task: Task<unknown, number> = Task.of((a, b, c) => a + b + c)
509
531
``` typescript
510
532
type ap = <E2 , S2 , S3 = S extends (arg : S2 ) => any ? ReturnType <S > : never >(
511
533
taskOrPromise : Task <E | E2 , S2 > | Promise <S2 >
512
- ) => Task <E | E2 , S3 >;
534
+ ) => Task <E | E2 , S3 >
513
535
` ` `
514
536
515
537
{% endtab %}
0 commit comments