我对promises还是相当陌生,目前正在使用bluebird,但是我有一个场景,我不太确定如何最好地处理它.
例如,我在一个express应用程序中有一个promise 链,比如:
repository.Query(getAccountByIdQuery)
.catch(function(error){
res.status(404).send({ error: "No account found with this Id" });
})
.then(convertDocumentToModel)
.then(verifyOldPassword)
.catch(function(error) {
res.status(406).send({ OldPassword: error });
})
.then(changePassword)
.then(function(){
res.status(200).send();
})
.catch(function(error){
console.log(error);
res.status(500).send({ error: "Unable to change password" });
});
所以我追求的行为是:
- 通过Id获取帐户
- 如果在这一点上有拒绝,爆炸出来并返回一个错误
- 如果没有错误,请将返回的文档转换为模型
- 使用数据库文档验证密码
- 如果密码不匹配,则弹出并返回另一个错误
- 如果没有错误,请更改密码
- 然后回归成功
- 如果还有其他问题,请返回500
因此,当前捕获似乎并没有停止链接,这是有道理的,所以我想知道是否有一种方法可以让我基于错误以某种方式迫使链在某个点停止,或者是否有更好的方法来构造它,以获得某种形式的分支行为,比如if X do Y else Z
.
任何帮助都会很好.