-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.js
45 lines (37 loc) · 817 Bytes
/
index.js
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
export default class PLazy extends Promise {
#executor;
#promise;
constructor(executor) {
super(resolve => {
resolve();
});
this.#executor = executor;
}
static from(function_) {
return new PLazy(resolve => {
resolve(function_());
});
}
static resolve(value) {
return new PLazy(resolve => {
resolve(value);
});
}
static reject(error) {
return new PLazy((resolve, reject) => {
reject(error);
});
}
then(onFulfilled, onRejected) {
this.#promise ??= new Promise(this.#executor);
return this.#promise.then(onFulfilled, onRejected);
}
catch(onRejected) {
this.#promise ??= new Promise(this.#executor);
return this.#promise.catch(onRejected);
}
finally(onFinally) {
this.#promise ??= new Promise(this.#executor);
return this.#promise.finally(onFinally);
}
}