-
Notifications
You must be signed in to change notification settings - Fork 7
/
async_either.ts
500 lines (466 loc) · 11.3 KB
/
async_either.ts
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
/**
* The AsyncEither datastructure represents an asynchronous operation that can
* fail. At its heart it is implemented as `() => Promise<Either<B, A>>`. This
* thunk makes it a performant but lazy operation at the expense of stack
* safety.
*
* @module AsyncEither
* @since 2.0.0
*/
import type { Kind, Out } from "./kind.ts";
import type { Applicable } from "./applicable.ts";
import type { Async } from "./async.ts";
import type { Bimappable } from "./bimappable.ts";
import type { Combinable } from "./combinable.ts";
import type { Either } from "./either.ts";
import type { Failable, Tap } from "./failable.ts";
import type { Bind, Flatmappable } from "./flatmappable.ts";
import type { Initializable } from "./initializable.ts";
import type { BindTo, Mappable } from "./mappable.ts";
import type { Wrappable } from "./wrappable.ts";
import * as E from "./either.ts";
import * as A from "./async.ts";
import * as P from "./promise.ts";
import { createTap } from "./failable.ts";
import { createBind } from "./flatmappable.ts";
import { createBindTo } from "./mappable.ts";
import { handleThrow, pipe } from "./fn.ts";
import { resolve } from "./promise.ts";
/**
* The AsyncEither type can best be thought of as an asynchronous function that
* returns an `Either`. ie. `async () => Promise<Either<B, A>>`. This
* forms the basis of most Promise based asynchronous communication in
* TypeScript.
*
* @since 2.0.0
*/
export type AsyncEither<L, R> = Async<Either<L, R>>;
/**
* Specifies AsyncEither as a Higher Kinded Type, with covariant
* parameter A corresponding to the 0th index of any substitutions and covariant
* parameter B corresponding to the 1st index of any substitutions.
*
* @since 2.0.0
*/
export interface KindAsyncEither extends Kind {
readonly kind: AsyncEither<Out<this, 1>, Out<this, 0>>;
}
/**
* Constructs a AsyncEither from a value and wraps it in an inner *Left*
* traditionally signaling a failure.
*
* @example
* ```ts
* import * as AE from "./async_either.ts";
*
* const left = AE.left(1);
*
* const result = await left(); // Left(1);
* ```
*
* @since 2.0.0
*/
export function left<B, A = never>(left: B): AsyncEither<B, A> {
return A.wrap(E.left(left));
}
/**
* Constructs a AsyncEither from a value and wraps it in an inner *Right*
* traditionally signaling a successful computation.
*
* @example
* ```ts
* import * as AE from "./async_either.ts";
*
* const right = AE.right(1);
*
* const result = await right(); // Right(1)
* ```
*
* @since 2.0.0
*/
export function right<A = never, B = never>(right: A): AsyncEither<B, A> {
return A.wrap(E.right(right));
}
/**
* Wraps a Async of A in a try-catch block which upon failure returns B instead.
* Upon success returns a *Right<A>* and *Left<B>* for a failure.
*
* @example
* ```ts
* import * as TE from "./async_either.ts";
* import * as E from "./either.ts";
*
* const tryFetch = TE.tryCatch(
* fetch,
* (error, args) => ({ message: "Fetch Error", error, args })
* );
*
* const result1 = await tryFetch("blah")(); // Left(ErrorStruct)
* const result2 = await tryFetch("https://deno.land/")(); // Right(*Deno Website*)
* ```
*
* @since 2.0.0
*/
export function tryCatch<AS extends unknown[], A, B>(
fasr: (...as: AS) => A | PromiseLike<A>,
onThrow: (e: unknown, as: AS) => B,
): (...as: AS) => AsyncEither<B, A> {
return (...as) => {
const _onThrow = (e: unknown) => E.left(onThrow(e, as));
return handleThrow(
() => fasr(...as),
(a) => resolve(a).then(E.right).catch(_onThrow),
(e) => resolve(_onThrow(e)),
);
};
}
/**
* Lift an always succeeding async computation (Async) into a AsyncEither.
*
* @example
* ```ts
* import * as AE from "./async_either.ts";
* import * as A from "./async.ts";
*
* const value = AE.fromAsync(A.wrap(1));
*
* const result1 = await value(); // Right(1)
* ```
*
* @since 2.0.0
*/
export function fromAsync<A, B = never>(ta: Async<A>): AsyncEither<B, A> {
return pipe(ta, A.map(E.right));
}
/**
* Lifts an Either<B,A> into a AsyncEither<B, A>.
*
* @example
* ```ts
* import * as AE from "./async_either.ts";
* import * as E from "./either.ts";
*
* const value1 = AE.fromEither(E.right(1));
* const value2 = AE.fromEither(E.left("Error!"));
*
* const result1 = await value1(); // Right(1)
* const result2 = await value2(); // Left("Error!")
* ```
*
* @since 2.0.0
*/
export function fromEither<A, B>(ta: Either<B, A>): AsyncEither<B, A> {
return () => resolve(ta);
}
/**
* Construct an AsyncEither<B, A> from a value A.
*
* @example
* ```ts
* import * as AE from "./async_either.ts";
*
* const value = AE.wrap(1);
*
* const result = await value(); // Right(1)
* ```
*
* @since 2.0.0
*/
export function wrap<A, B = never>(a: A): AsyncEither<B, A> {
return right(a);
}
/**
* Construct an AsyncEither<B, A> from a value B.
*
* @example
* ```ts
* import * as AE from "./async_either.ts";
*
* const value = AE.fail("Error!");
*
* const result = await value(); // Left("Error!");
* ```
*
* @since 2.0.0
*/
export function fail<A = never, B = never>(b: B): AsyncEither<B, A> {
return left(b);
}
/**
* Map a function over the *Right* side of a AsyncEither
*
* @since 2.0.0
*/
export function map<A, I>(
fai: (a: A) => I,
): <B>(ta: AsyncEither<B, A>) => AsyncEither<B, I> {
return (ta) => pipe(ta, A.map(E.map(fai)));
}
/**
* Map a function over the *Left* side of a AsyncEither
*
* @since 2.0.0
*/
export function mapSecond<B, J>(
fbj: (b: B) => J,
): <A>(ta: AsyncEither<B, A>) => AsyncEither<J, A> {
return (ta) => pipe(ta, A.map(E.mapSecond(fbj)));
}
/**
* Apply an argument to a function under the *Right* side.
*
* @since 2.0.0
*/
export function apply<A, B>(
ua: AsyncEither<B, A>,
): <I, J>(ufai: AsyncEither<J, (a: A) => I>) => AsyncEither<B | J, I> {
return (ufai) => () =>
pipe(
P.all(ufai(), ua()),
P.map(([efai, ea]) => pipe(efai, E.apply(ea))),
);
}
/**
* Sequentially apply arguments
*
* @since 2.0.0
*/
export function applySequential<A, B>(
ua: AsyncEither<B, A>,
): <I, J = never>(ufai: AsyncEither<J, (a: A) => I>) => AsyncEither<B | J, I> {
return (ufai) => async () => {
const ea = await ua();
const efai = await ufai();
return pipe(efai, E.apply(ea));
};
}
/**
* Chain AsyncEither based computations together in a pipeline
*
* ```ts
* import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
* import * as TE from "./async_either.ts";
* import * as E from "./either.ts";
* import { pipe } from "./fn.ts";
*
* const ta = pipe(
* TE.wrap(1),
* TE.flatmap(n => TE.wrap(n*2)),
* TE.flatmap(n => TE.wrap(n**2))
* )
*
* assertEquals(await ta(), E.right(4))
* ```
*
* @since 2.0.0
*/
export function flatmap<A, I, J>(
fati: (a: A) => AsyncEither<J, I>,
): <B>(ta: AsyncEither<B, A>) => AsyncEither<B | J, I> {
return (ta) => async () => {
const ea = await ta();
return E.isLeft(ea) ? ea : fati(ea.right)();
};
}
/**
* @since 2.0.0
*/
export function flatmapFirst<A, I, J>(
fati: (a: A) => AsyncEither<J, I>,
): <B>(ta: AsyncEither<B, A>) => AsyncEither<B | J, A> {
return (ta) => async () => {
const ea = await ta();
if (E.isLeft(ea)) {
return ea;
} else {
const ei = await fati(ea.right)();
return E.isLeft(ei) ? ei : ea;
}
};
}
/**
* Chain AsyncEither based failures, *Left* sides, useful for recovering
* from error conditions.
*
* ```ts
* import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
* import * as TE from "./async_either.ts";
* import * as E from "./either.ts";
* import { pipe } from "./fn.ts";
*
* const ta = pipe(
* TE.fail(1),
* TE.recover(n => TE.wrap(n*2)),
* TE.flatmap(n => TE.wrap(n**2))
* )
*
* assertEquals(await ta(), E.right(4))
* ```
*
* @since 2.0.0
*/
export function recover<B, J, I>(
fbtj: (b: B) => AsyncEither<J, I>,
): <A>(ta: AsyncEither<B, A>) => AsyncEither<J, A | I> {
return (ta) => async () => {
const ea = await ta();
return E.isLeft(ea) ? fbtj(ea.left)() : ea;
};
}
/**
* Provide an alternative for a failed computation.
* Useful for implementing defaults.
*
* ```ts
* import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
* import * as TE from "./async_either.ts";
* import * as E from "./either.ts";
* import { pipe } from "./fn.ts";
*
* const ta = pipe(
* TE.fail(1),
* TE.alt(TE.wrap(2)),
* )
*
* assertEquals(await ta(), E.right(2))
* ```
*
* @since 2.0.0
*/
export function alt<I, J>(
ti: AsyncEither<J, I>,
): <A, B>(ta: AsyncEither<B, A>) => AsyncEither<B | J, A | I> {
return (ta) => async () => {
const ea = await ta();
return E.isLeft(ea) ? ti() : ea;
};
}
/**
* Fold away the inner Either from the `AsyncEither` leaving us with the
* result of our computation in the form of a `Async`
*
* @since 2.0.0
*/
export function match<L = unknown, R = unknown, B = never>(
onLeft: (left: L) => B,
onRight: (right: R) => B,
): (ta: AsyncEither<L, R>) => Async<B> {
return (ta) => () => ta().then(E.match<L, R, B>(onLeft, onRight));
}
// This leaks async ops so we cut it for now.
//export const timeout = <E, A>(ms: number, onTimeout: () => E) =>
// (ta: AsyncEither<E, A>): AsyncEither<E, A> =>
// () => Promise.race([ta(), wait(ms).then(flow(onTimeout, E.left))]);
/**
* @since 2.0.0
*/
export function getCombinableAsyncEither<A, B>(
CA: Combinable<A>,
CB: Combinable<B>,
): Combinable<AsyncEither<B, A>> {
const { combine } = E.getCombinableEither(CA, CB);
return {
combine: (second) => (first) => async () =>
combine(await second())(await first()),
};
}
/**
* @since 2.0.0
*/
export function getInitializableAsyncEither<A, B>(
CA: Initializable<A>,
CB: Initializable<B>,
): Initializable<AsyncEither<B, A>> {
const { init, combine } = E.getInitializableEither(CA, CB);
return {
init: () => () => resolve(init()),
combine: (second) => (first) => async () =>
combine(await second())(await first()),
};
}
/**
* @since 2.0.0
*/
export const ApplicableAsyncEither: Applicable<KindAsyncEither> = {
apply,
map,
wrap,
};
/**
* @since 2.0.0
*/
export const BimappableAsyncEither: Bimappable<KindAsyncEither> = {
map,
mapSecond,
};
/**
* @since 2.0.0
*/
export const FlatmappableAsyncEitherParallel: Flatmappable<KindAsyncEither> = {
wrap,
apply,
map,
flatmap,
};
/**
* @since 2.0.0
*/
export const FlatmappableAsyncEitherSequential: Flatmappable<KindAsyncEither> =
{
wrap,
apply: applySequential,
map,
flatmap,
};
/**
* @since 2.0.0
*/
export const FailableAsyncEitherParallel: Failable<KindAsyncEither> = {
wrap,
apply,
map,
flatmap,
alt,
fail,
recover,
};
/**
* @since 2.0.0
*/
export const FailableAsyncEitherSequential: Failable<KindAsyncEither> = {
wrap,
apply: applySequential,
map,
flatmap,
alt,
fail,
recover,
};
/**
* @since 2.0.0
*/
export const MappableAsyncEither: Mappable<KindAsyncEither> = {
map,
};
/**
* @since 2.0.0
*/
export const WrappableAsyncEither: Wrappable<KindAsyncEither> = {
wrap,
};
/**
* @since 2.0.0
*/
export const tap: Tap<KindAsyncEither> = createTap(FailableAsyncEitherParallel);
/**
* @since 2.0.0
*/
export const bind: Bind<KindAsyncEither> = createBind(
FlatmappableAsyncEitherParallel,
);
/**
* @since 2.0.0
*/
export const bindTo: BindTo<KindAsyncEither> = createBindTo(
FlatmappableAsyncEitherParallel,
);