// https://github.com/facebook/react/issues/5465 export interface CancelablePromise { promise: Promise; cancel: () => void; } export const makePromiseCancelable = (promise: Promise): CancelablePromise => { 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; }, }; };