Last active
August 1, 2019 08:47
-
-
Save brandonweis/1e4d0df727f63947feef5fac9da9fb02 to your computer and use it in GitHub Desktop.
This file contains hidden or 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