Provides an implementation of a cancellable promise and the corresponding TypeScript interfaces
import { CancellablePromise, CancelledPromiseError} from '@ef-carbon/promise';
const promise = new CancellablePromise<number>((resolve, reject, onCancel) => {
onCancel(() => console.log('cancelled'));
someLongAsyncOperation().then(resolve, reject);
});
promise.cancel(); // stdout: 'cancelled'
promise.cancelled(); // -> true
promise.isCancelled(); // -> true
promise.catch(error => {
if (error instanceof CancelledPromiseError) {
console.log('promise was cancelled');
}
});