Skip to content

Instantly share code, notes, and snippets.

@ohtangza
Created February 27, 2018 02:52
Show Gist options
  • Save ohtangza/f3cd86ee113b89be8019a5bb09132b2f to your computer and use it in GitHub Desktop.
Save ohtangza/f3cd86ee113b89be8019a5bb09132b2f to your computer and use it in GitHub Desktop.
Bluebird.Promise cancellation
import Promise from 'bluebird';
Promise.config({
cancellation: true,
});
console.log('Experiment A!!!');
const rootPromise1 = new Promise((resolve, reject, onCancel) => {
console.log('A: rootPromise1 started');
setTimeout(() => {
resolve();
}, 100);
onCancel(() => {
console.log('A: rootPromise1 cancelled');
});
return Promise.resolve()
.then(() => new Promise((resolve, reject, onCancel) => {
console.log('A: subPromise1 started');
setTimeout(() => {
resolve();
}, 100);
onCancel(() => {
console.log('A: subPromise1 cancelled');
});
}))
.then(() => new Promise((resolve, reject, onCancel) => {
console.log('A: subPromise2 started');
setTimeout(() => {
resolve();
}, 100);
onCancel(() => {
console.log('A: subPromise2 cancelled');
});
}));
});
rootPromise1.cancel();
import Promise from 'bluebird';
Promise.config({
cancellation: true,
});
console.log('Experiment B!!!');
const rootPromise2 = Promise.resolve()
.then(() => new Promise((resolve, reject, onCancel) => {
console.log('B: subPromise1 started');
setTimeout(() => {
resolve();
}, 100);
onCancel(() => {
console.log('B: subPromise1 cancelled');
});
}))
.then(() => new Promise((resolve, reject, onCancel) => {
console.log('B: subPromise2 started');
setTimeout(() => {
resolve();
}, 100);
onCancel(() => {
console.log('B: subPromise2 cancelled');
});
}));
setTimeout(() => {
rootPromise2.cancel();
}, 150);
@ohtangza
Copy link
Author

screen shot 2018-02-27 at 11 53 24 am

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment