【发布时间】:2014-07-07 20:38:43
【问题描述】:
我正在使用 bluebird Promise 库。我想链接承诺并捕获特定的承诺错误。这就是我正在做的事情:
getSession(sessionId)
.catch(function (err) {
next(new Error('session not found'));
})
.then(function (session) {
return getUser(session.user_id);
})
.catch(function (err) {
next(new Error('user not found'));
})
.then(function (user) {
req.user = user;
next();
});
但如果getSession 抛出错误,则调用两个catch,以及第二个then。我想在第一个catch 停止错误传播,这样第二个catch 仅在getUser 抛出时调用,第二个then 在getUser 成功时调用。做什么?
【问题讨论】:
-
如果出现错误,为什么要调用 next()?有没有像 next() 这样的 abort() 可以使用?
-
我正在使用快递。
next调用下一个中间件。如果你给它传递一个参数,那么它会跳到错误中间件。
标签: javascript bluebird