Reprinted from to cancel the Promise plug in the wings
const makeCancelable = (promise) => { let hasCanceled_ = false; const wrappedPromise = new Promise((resolve, reject) => { promise.then((val) => hasCanceled_ ? reject({isCanceled: true}) : resolve(val) ); promise.catch((error) => hasCanceled_ ? reject({isCanceled: true}) : reject(error) ); }); return { promise: wrappedPromise, cancel() { hasCanceled_ = true; }, }; };
// Call const somePromise = new Promise (r = > setTimeout (r, 1000)); // Create an asynchronous operation const cancelable = makeCancelable (somePromise); // add functionality to cancel the asynchronous operation cancelable .promise .then ( () => the console.log ( 'resolved')) .catch (({isCanceled, ...} error) => the console.log ( 'isCanceled', isCanceled)); // cancel the asynchronous operation cancelable.cancel () ;