@newdash/newdash
Version:
javascript/typescript utility library
45 lines (44 loc) • 1.04 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.LazyPromise = void 0;
/**
* @internal
*/
const GlobalPromise = Promise;
/**
* LazyPromise, execute async operation when user await it.
*
* @author Theo Sun
*
* @category Async
* @since 5.18.0
*/
class LazyPromise {
_executor;
_promise;
/**
* LazyPromise, execute async operation when user await it.
*
* @param executor Promise executor
*/
constructor(executor) {
this._executor = executor;
}
[Symbol.toStringTag];
_getPromise() {
if (this._promise === undefined) {
this._promise = new GlobalPromise(this._executor);
}
return this._promise;
}
then(onfulfilled, onrejected) {
return this._getPromise().then(onfulfilled, onrejected);
}
catch(onrejected) {
return this._getPromise().catch(onrejected);
}
finally(onfinally) {
return this._getPromise().finally(onfinally);
}
}
exports.LazyPromise = LazyPromise;