-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
promises.ts
79 lines (74 loc) 路 1.93 KB
/
promises.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
// Copyright 2019 Yusuke Sakurai. All rights reserved. MIT license.
export type Deferred<T = any, R = Error> = {
promise: Promise<T>;
resolve: (t?: T) => void;
reject: (r?: R) => void;
readonly handled: boolean;
};
/** Create deferred promise that can be resolved and rejected by outside */
export function defer<T = void>(): Deferred<T> {
let handled = false;
let resolve;
let reject;
const promise = new Promise<T>((res, rej) => {
resolve = r => {
handled = true;
res(r);
};
reject = r => {
handled = true;
rej(r);
};
});
return {
promise,
resolve,
reject,
get handled() {
return handled;
}
};
}
export async function rejectify<T>(promise: Promise<T>): Promise<never> {
return new Promise<never>((resolve, reject) => {
promise.then(reject).catch(reject);
});
}
export async function takeFirst<T>(promise: Promise<T>, ...racers): Promise<T> {
return new Promise((resolve, reject) => {
promise.then(resolve).catch(reject);
racers.forEach(r => r.then(reject).catch(reject));
});
}
export class TimeoutError extends Error {}
/** returns curried promise factory that */
export function promiseInterrupter(opts: {
timeout?: number;
cancel?: Promise<void>;
}): <T>(p: Promise<T>) => Promise<T> {
let { timeout, cancel } = opts;
timeout = Number.isInteger(timeout) ? timeout : -1;
return <T>(p) =>
new Promise<T>((resolve, reject) => {
if (timeout < 0) {
p.then(resolve).catch(reject);
if (cancel) {
cancel.then(reject).catch(reject);
}
} else {
const i = setTimeout(() => {
reject(new TimeoutError());
}, timeout);
const clear = () => clearTimeout(i);
p.then(resolve)
.catch(reject)
.finally(clear);
if (cancel) {
cancel
.then(reject)
.catch(reject)
.finally(clear);
}
}
});
}