@xylabs/promise
Version:
Base functionality used throughout XY Labs TypeScript/JavaScript libraries
33 lines (28 loc) • 1.05 kB
text/typescript
export type PromiseExSubFunc<T, TResult = T> = (value: T) => TResult
export type PromiseExFunc<T> = (resolve?: PromiseExSubFunc<T, void>, reject?: PromiseExSubFunc<T, void>) => void
export type PromiseExValueFunc<V> = (value?: V) => boolean
export class PromiseEx<T, V = void> extends Promise<T> {
cancelled?: boolean
private _value?: V
constructor(func: PromiseExFunc<T>, value?: V) {
super(func)
this._value = value
}
// eslint-disable-next-line unicorn/no-thenable
override then<TResult1 = T, TResult2 = never>(
onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | null | undefined,
onrejected?: ((reason: unknown) => TResult2 | PromiseLike<TResult2>) | null | undefined,
onvalue?: (value?: V) => boolean,
): Promise<TResult1 | TResult2> {
if (onvalue?.(this._value)) {
this.cancelled = true
}
return super.then(onfulfilled, onrejected)
}
value(onvalue?: (value?: V) => boolean) {
if (onvalue?.(this._value)) {
this.cancelled = true
}
return this
}
}