Last active
August 14, 2019 23:20
-
-
Save cecigarcia/ec38e87e71cc13e6a7e59a01a84ecb5c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const cancellablePromise = promise => { | |
let isCanceled = false; | |
const wrappedPromise = new Promise((resolve, reject) => { | |
promise.then( | |
value => (isCanceled ? reject({ isCanceled, value }) : resolve(value)), | |
error => reject({ isCanceled, error }), | |
); | |
}); | |
return { | |
promise: wrappedPromise, | |
cancel: () => (isCanceled = true), | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment