Pb is little, but powerfull utility module made to close some gaps in JavaScript Promises. Please feel free to provide any requests to new functionality. It is based on petkaantonov/bluebird#581 issue.
$ npm install --save promise-chain-break
const pb = require('promise-chain-break');
To make magic work you need to wrap over Promise callbacks with pb
at least at one level lower then you planing to pass flow control commands and to the end of chain.
But it is better for code maintance to wrap all callbacks at once.
db.getData()
.then(pb((data) => {
}))
.then(pb(() => {
}))
.then(pb(() => {
}))
.catch((error) => {
console.error(error);
});
If you need to break a chain of promises
db.getData()
.then(pb((data) => {
if (!data.someCheck()) {
tellSomeone();
// All other '.then' calls will be skiped
return pb.BREAK;
}
}))
.then(pb(() => {
}))
.then(pb(() => {
}))
.catch((error) => {
console.error(error);
});
If you decided to use one call in chain only in some cases.
db.getData()
.then(pb((data) => {
if (!data.needSomeMoreDBWork) {
// Next '.then' call will be skiped
return pb.SKIP_NEXT;
}
}))
.then(pb(() => {
return db.doMoreStuff();
}))
.then(pb(() => {
}))
.catch((error) => {
console.error(error);
});