Skip to content

Instantly share code, notes, and snippets.

@bennycode
Last active February 12, 2020 15:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bennycode/adcbc040916072ca8b22d0c5930ae670 to your computer and use it in GitHub Desktop.
Save bennycode/adcbc040916072ca8b22d0c5930ae670 to your computer and use it in GitHub Desktop.
Bluebird Promise Cancellation
// Compliant Promise/A+ implementation: https://www.promisejs.org/implementing/
// Alternative implementation: https://dexie.org/docs/Promise/Promise
// Checkout the Node.js Event Loop: https://www.youtube.com/watch?v=PNa9OMajw9w
// Bluebird breaking change: http://bluebirdjs.com/docs/new-in-bluebird-3.html#cancellation-overhaul
const Promise = require('bluebird');
Promise.config({
cancellation: true,
});
function getPromise() {
return new Promise((resolve, reject, onCancel) => {
const handle = setTimeout(() => {
console.log('I am resolving now...');
resolve();
}, 5000);
onCancel(() => {
console.log('I am getting canceled.');
clearTimeout(handle);
});
});
}
(async () => {
const promiseReference = getPromise();
promiseReference.cancel();
console.log('Program exits.');
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment