Using node
, restify
and bluebird promises
and a sleep-deprived mind:
let chain = Promise.try(() => {
return AsyncCall(auth_token).then(result => {
if (!result)
throw new Error('AUTH_TOKEN_ERROR');
} else {
return facebook_data;
}
});
}).catch((error) => {
res.code(401).error(error.message);
next();
});
chain.then((result) => {
// Gets called even though we throw the error.
});
My problem is that I really like the chain.then()-method of "ending" the promise chain (for readability), but it gets called even though .catch gets an error. I realize I can set the final .then() into the first chain block, but I want to understand this.
How can I keep the same code structure, but having a .catch()-event end the promise execution flow?