Created
October 30, 2018 13:43
-
-
Save bennycode/cd0f5b78b8824fe81357340b747347cd to your computer and use it in GitHub Desktop.
Promise cancellation with bluebird 3.x
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 Promise = require('bluebird'); | |
Promise.config({ | |
cancellation: true | |
}); | |
const TIME_TO_WAIT_FOR_PROMISE = 1000; | |
const delayedPromise = new Promise((resolve, reject) => | |
setTimeout(() => { | |
if (delayedPromise.isCancelled()) return; | |
const message = 'I will throw an Error if nobody cancels me.'; | |
const error = new Error(message); | |
console.error(error.message); | |
reject(error); | |
}, TIME_TO_WAIT_FOR_PROMISE) | |
); | |
delayedPromise.cancel(); // If this line gets removed, the program will fail. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment