1 | class LazyPromise {
|
2 | constructor(func, params) {
|
3 | this._func = func;
|
4 | this._params = params;
|
5 | this._promise = undefined; // not call `func(...params)` immediately
|
6 | }
|
7 |
|
8 | async then(resolve, reject) {
|
9 | this._promise = this._promise || this._func(...this._params);
|
10 |
|
11 | try {
|
12 | resolve(await this._promise);
|
13 | } catch (e) {
|
14 | reject(e);
|
15 | }
|
16 | }
|
17 | }
|
18 |
|
19 | module.exports = LazyPromise;
|