Created
October 26, 2016 20:31
-
-
Save theseyi/748dbaf8bc3aab614dd2b47633fba55b to your computer and use it in GitHub Desktop.
Cancelable Promise
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
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; | |
}, | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment