使用node
,restify
和bluebird promises
以及睡眠不足的头脑:
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.
});
我的问题是我真的很喜欢"结束"承诺链的chain.then()-方法(为了可读性),但即使.catch收到错误,它也会被调用。我意识到我可以将最终的 .then() 设置为第一个链块,但我想了解这一点。
如何保持相同的代码结构,但让 .catch() 事件结束承诺执行流程?
catch
假设您正在处理错误。如果你不是并希望在以后的catch
处理它,请像这样重新抛出它:
.catch((error) => {
res.code(401).error(error.message);
next();
throw error;
});