Skip to content

Instantly share code, notes, and snippets.

@bennycode
Created October 30, 2018 13:43
Show Gist options
  • Save bennycode/cd0f5b78b8824fe81357340b747347cd to your computer and use it in GitHub Desktop.
Save bennycode/cd0f5b78b8824fe81357340b747347cd to your computer and use it in GitHub Desktop.
Promise cancellation with bluebird 3.x
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