@nesvet/n
Version:
Various utilities
46 lines • 1.42 kB
JavaScript
/* eslint-disable @typescript-eslint/no-explicit-any */
const nativesMap = new WeakMap();
export class StatefulPromise extends Promise {
constructor(executor) {
let nativeResolve;
let nativeReject;
super((resolve, reject) => {
nativeResolve = resolve;
nativeReject = reject;
});
nativesMap.set(this, { resolve: nativeResolve, reject: nativeReject });
executor?.(this.resolve, this.reject);
}
isPending = true;
isFulfilled = false;
isRejected = false;
state = "pending";
result;
resolve = (value) => {
if (this.isPending) {
this.isPending = false;
this.isFulfilled = true;
this.state = "fulfilled";
this.result = value;
const native = nativesMap.get(this);
if (native) {
native.resolve.call(this, value);
nativesMap.delete(this);
}
}
};
reject = (reason) => {
if (this.isPending) {
this.isPending = false;
this.isRejected = true;
this.state = "rejected";
this.result = reason;
const native = nativesMap.get(this);
if (native) {
native.reject.call(this, reason);
nativesMap.delete(this);
}
}
};
}
//# sourceMappingURL=StatefulPromise.js.map