-
Notifications
You must be signed in to change notification settings - Fork 0
/
promise.ts
146 lines (138 loc) · 3.69 KB
/
promise.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
import type { Unary } from './data.ts'
import type { Result } from './result.ts'
export function next_frame(): Promise<void> {
return new Promise((yes) => {
requestAnimationFrame(() => yes())
})
}
export function next_tick(): Promise<void> {
return new Promise((yes) => yes())
}
export function sleep(n: number): Promise<void> {
return new Promise(yes => setTimeout(() => yes(), n))
}
export function map<A, B>(f: Unary<A, B>): (x: Promise<A>) => Promise<B> {
return function(x) {
return x.then(f)
}
}
export function bind<A, B>(f: Unary<A, Promise<B>>): (x: Promise<A>) => Promise<B> {
return function(x) {
return x.then(f)
}
}
export function then<A, B>(f: Unary<A, B>): (x: Promise<A>) => Promise<B>
export function then<A, B>(f: Unary<A, Promise<B>>): (x: Promise<A>) => Promise<B> {
return function(x) {
return x.then(f)
}
}
/** pipe an argument through a number of functions, all of which return promises
*
* return the result of the final function as a promise
*/
export function pipe<A, B>(
x: A,
a: Unary<A, Promise<B>>,
): Promise<B>
export function pipe<A, B, C>(
x: A,
a: Unary<A, Promise<B>>,
b: Unary<B, Promise<C>>,
): Promise<C>
export function pipe<A, B, C, D>(
x: A,
a: Unary<A, Promise<B>>,
b: Unary<B, Promise<C>>,
c: Unary<C, Promise<D>>,
): Promise<D>
export function pipe<A, B, C, D, E>(
x: A,
a: Unary<A, Promise<B>>,
b: Unary<B, Promise<C>>,
c: Unary<C, Promise<D>>,
d: Unary<D, Promise<E>>,
): Promise<E>
export function pipe<A, B, C, D, E, F>(
x: A,
a: Unary<A, Promise<B>>,
b: Unary<B, Promise<C>>,
c: Unary<C, Promise<D>>,
d: Unary<D, Promise<E>>,
e: Unary<E, Promise<F>>,
): Promise<F>
export function pipe<A, B, C, D, E, F, G>(
x: A,
a: Unary<A, Promise<B>>,
b: Unary<B, Promise<C>>,
c: Unary<C, Promise<D>>,
d: Unary<D, Promise<E>>,
e: Unary<E, Promise<F>>,
f: Unary<F, Promise<G>>,
): Promise<G>
export function pipe<A, B, C, D, E, F, G, H>(
x: A,
a: Unary<A, Promise<B>>,
b: Unary<B, Promise<C>>,
c: Unary<C, Promise<D>>,
d: Unary<D, Promise<E>>,
e: Unary<E, Promise<F>>,
f: Unary<F, Promise<G>>,
g: Unary<G, Promise<H>>,
): Promise<H>
export function pipe<A, B, C, D, E, F, G, H, I>(
x: A,
a: Unary<A, Promise<B>>,
b: Unary<B, Promise<C>>,
c: Unary<C, Promise<D>>,
d: Unary<D, Promise<E>>,
e: Unary<E, Promise<F>>,
f: Unary<F, Promise<G>>,
g: Unary<G, Promise<H>>,
h: Unary<H, Promise<I>>,
): Promise<I>
export function pipe<A, B, C, D, E, F, G, H, I, J>(
x: A,
a: Unary<A, Promise<B>>,
b: Unary<B, Promise<C>>,
c: Unary<C, Promise<D>>,
d: Unary<D, Promise<E>>,
e: Unary<E, Promise<F>>,
f: Unary<F, Promise<G>>,
g: Unary<G, Promise<H>>,
h: Unary<H, Promise<I>>,
i: Unary<I, Promise<J>>,
): Promise<J>
export function pipe<A, B, C, D, E, F, G, H, I, J, K>(
x: A,
a: Unary<A, Promise<B>>,
b: Unary<B, Promise<C>>,
c: Unary<C, Promise<D>>,
d: Unary<D, Promise<E>>,
e: Unary<E, Promise<F>>,
f: Unary<F, Promise<G>>,
g: Unary<G, Promise<H>>,
h: Unary<H, Promise<I>>,
i: Unary<I, Promise<J>>,
j: Unary<J, Promise<K>>,
): Promise<K>
export async function pipe(x: any, ...fs: Array<Unary<any, Promise<any>>>): Promise<any> {
let a = x
for (const f of fs)
a = await f(a)
return a
}
export async function retry_exponential_backoff<T>(f: () => Promise<T>, initial = 500, multiplier = 2, max_retries = Infinity): Promise<Result<T>> {
let i = 0
let t = initial
while (i <= max_retries) {
const x = await f().catch(e => e instanceof Error ? e : new Error('something went wrong: ' + e));
if (x instanceof Error) {
await sleep(t)
t *= multiplier
i++
} else
return x
}
return new Error('max retries exceeded')
}