class CancelablePromiseInternal < T = any > { #internals : Internals ; #promise : Promise < T > ; constructor ( { executor = ( ) => { } , internals = defaultInternals ( ) , promise = new Promise < T > ( ( resolve , reject ) => executor ( resolve , reject , ( onCancel ) => { internals . onCancelList . push ( onCancel ) ; } ) ) , } ) { this . cancel = this . cancel . bind ( this ) ; this . #internals = internals ; this . #promise = promise || new Promise < T > ( ( resolve , reject ) => executor ( resolve , reject , ( onCancel ) => { internals . onCancelList . push ( onCancel ) ; } ) ) ; } then < TResult1 = T , TResult2 = never > ( onfulfilled ? , onrejected ? ) { return makeCancelable < TResult1 | TResult2 > ( this . #promise . then ( createCallback ( onfulfilled , this . #internals ) , createCallback ( onrejected , this . #internals ) ) , this . #internals ) ; } catch < TResult = never > ( onrejected ? ) { return makeCancelable < T | TResult > ( this . #promise . catch ( createCallback ( onrejected , this . #internals ) ) , this . #internals ) ; } finally ( onfinally ? , runWhenCanceled ? ) { if ( runWhenCanceled ) { this . #internals . onCancelList . push ( onfinally ) ; } return makeCancelable < T > ( this . #promise . finally ( createCallback ( ( ) => { if ( onfinally ) { if ( runWhenCanceled ) { this . #internals . onCancelList = this . #internals . onCancelList . filter ( ( callback ) => callback !== onfinally ) ; } return onfinally ( ) ; } } , this . #internals ) ) , this . #internals ) ; } cancel ( ) { this . #internals . isCanceled = true ; const callbacks = this . #internals . onCancelList ; this . #internals . onCancelList = [ ] ; for ( const callback of callbacks ) { if ( typeof callback === 'function' ) { try { callback ( ) ; } catch ( err ) { console . error ( err ) ; } } } } isCanceled ( ) { return this . #internals . isCanceled === true ; } } export class CancelablePromise < T = any > extends CancelablePromiseInternal < T > { static all = function all ( iterable ) { return makeAllCancelable ( iterable , Promise . all ( iterable ) ) ; } as CancelablePromiseOverloads [ 'all' ] ; static allSettled = function allSettled ( iterable ) { return makeAllCancelable ( iterable , Promise . allSettled ( iterable ) ) ; } as CancelablePromiseOverloads [ 'allSettled' ] ; static race = function race ( iterable ) { return makeAllCancelable ( iterable , Promise . race ( iterable ) ) ; } as CancelablePromiseOverloads [ 'race' ] ; static resolve = function resolve ( value ) { return cancelable ( Promise . resolve ( value ) ) ; } as CancelablePromiseOverloads [ 'resolve' ] ; static reject = function reject ( reason ) { return cancelable ( Promise . reject ( reason ) ) ; } as CancelablePromiseOverloads [ 'reject' ] ; static isCancelable = isCancelablePromise ; constructor ( executor ) { super ( { executor } ) ; } } export default CancelablePromise ; export function cancelable < T = any > ( promise ) { return makeCancelable ( promise , defaultInternals ( ) ) ; } export function isCancelablePromise ( promise ) { return ( promise instanceof CancelablePromise || promise instanceof CancelablePromiseInternal ) ; } function createCallback ( onResult , internals ) { if ( onResult ) { return ( arg ? ) => { if ( ! internals . isCanceled ) { const result = onResult ( arg ) ; if ( isCancelablePromise ( result ) ) { internals . onCancelList . push ( result . cancel ) ; } return result ; } return arg ; } ; } } function makeCancelable < T > ( promise , internals ) { return new CancelablePromiseInternal < T > ( { internals , promise , } ) as CancelablePromise < T > ; } function makeAllCancelable ( iterable , promise ) { const internals = defaultInternals ( ) ; internals . onCancelList . push ( ( ) => { for ( const resolvable of iterable ) { if ( isCancelablePromise ( resolvable ) ) { resolvable . cancel ( ) ; } } } ) ; return new CancelablePromiseInternal ( { internals , promise } ) ; } function defaultInternals ( ) { return { isCanceled : false , onCancelList : [ ] } ; } interface Internals { isCanceled : boolean ; onCancelList : any [ ] ; } interface CancelablePromiseOverloads { all < T1 , T2 , T3 , T4 , T5 , T6 , T7 , T8 , T9 , T10 , T11 , T12 > ( values ) : CancelablePromise < [ T1 , T2 , T3 , T4 , T5 , T6 , T7 , T8 , T9 , T10 , T11 , T12 ] > ; all < T1 , T2 , T3 , T4 , T5 , T6 , T7 , T8 , T9 , T10 , T11 > ( values ) : CancelablePromise < [ T1 , T2 , T3 , T4 , T5 , T6 , T7 , T8 , T9 , T10 , T11 ] > ; all < T1 , T2 , T3 , T4 , T5 , T6 , T7 , T8 , T9 , T10 > ( values ) : CancelablePromise < [ T1 , T2 , T3 , T4 , T5 , T6 , T7 , T8 , T9 , T10 ] > ; all < T1 , T2 , T3 , T4 , T5 , T6 , T7 , T8 , T9 > ( values ) : CancelablePromise < [ T1 , T2 , T3 , T4 , T5 , T6 , T7 , T8 , T9 ] > ; all < T1 , T2 , T3 , T4 , T5 , T6 , T7 , T8 > ( values ) : CancelablePromise < [ T1 , T2 , T3 , T4 , T5 , T6 , T7 , T8 ] > ; all < T1 , T2 , T3 , T4 , T5 , T6 , T7 > ( values ) : CancelablePromise < [ T1 , T2 , T3 , T4 , T5 , T6 , T7 ] > ; all < T1 , T2 , T3 , T4 , T5 , T6 > ( values ) : CancelablePromise < [ T1 , T2 , T3 , T4 , T5 , T6 ] > ; all < T1 , T2 , T3 , T4 , T5 > ( values ) : CancelablePromise < [ T1 , T2 , T3 , T4 , T5 ] > ; all < T1 , T2 , T3 , T4 > ( values ) : CancelablePromise < [ T1 , T2 , T3 , T4 ] > ; all < T1 , T2 , T3 > ( values ) : CancelablePromise < [ T1 , T2 , T3 ] > ; all < T1 , T2 > ( values ) : CancelablePromise < [ T1 , T2 ] > ; all < T > ( values ) : CancelablePromise < T [ ] > ; allSettled < T extends readonly unknown [ ] | readonly [ unknown ] > ( values ) : CancelablePromise < { - readonly [ P in keyof T ] : PromiseSettledResult < T [ P ] extends PromiseLike < infer U > ? U : T [ P ] > ; } > ; allSettled < T > ( values ) : CancelablePromise < PromiseSettledResult < T extends PromiseLike < infer U > ? U : T > [ ] > ; race < T > ( values ) : CancelablePromise < T extends PromiseLike < infer U > ? U : T > ; resolve ( ) : CancelablePromise < void > ; resolve < T > ( value ) : CancelablePromise < T > ; reject < T = never > ( reason ? ) : CancelablePromise < T > ; }