class CancelledPromiseError extends Error { } /**
 * Cancellable promise wrapper
 */ export const cancellablePromise = < ReturnType > ( promise ) => { let isCancelled = false ; const wrappedPromise = new Promise < ReturnType > ( ( res , rej ) => { promise . then ( ( value ) => { if ( isCancelled ) { throw new CancelledPromiseError ( ) ; } else { res ( value ) ; } } ) . catch ( ( error ) => { if ( ! ( error instanceof CancelledPromiseError ) ) { rej ( error ) ; } } ) ; } ) ; return { promise : wrappedPromise , cancel : ( ) => { isCancelled = true ; } , } ; } ;