@cloudpss/ubrpc
Version:
51 lines • 1.27 kB
JavaScript
/** Ready Promise */
export class ReadyPromise {
constructor() {
let resolve, reject;
const p = new Promise((res, rej) => {
resolve = res;
reject = rej;
});
p.catch(() => {
// Avoid unhandled rejection
});
this.#value = p;
this.#resolve = resolve;
this.#reject = reject;
}
#value;
#resolve;
#reject;
#status = 'pending';
/** Resolve the promise */
resolve() {
if (this.#status !== 'pending')
return;
this.#status = 'fulfilled';
this.#resolve();
}
/** Reject the promise */
reject(reason) {
if (this.#status !== 'pending')
return;
this.#status = 'rejected';
this.#reject(reason);
}
/** Settle the promise by provided promise */
settle(p) {
p.then(() => this.resolve(), (err) => this.reject(err));
}
/** Get the status of the promise */
get status() {
return this.#status;
}
/** Check if the promise is settled */
get settled() {
return this.#status !== 'pending';
}
/** Get the underlying promise */
get value() {
return this.#value;
}
}
//# sourceMappingURL=ready.js.map