Last active
February 12, 2020 15:26
-
-
Save bennycode/adcbc040916072ca8b22d0c5930ae670 to your computer and use it in GitHub Desktop.
Bluebird Promise Cancellation
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
// 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