javascript - 在链式承诺的每一步评估价值并打破承诺
问题描述
我有以下连锁承诺。在每一步,我都需要评估返回的值是否不为空。我可以在每一步添加一个 if else 条件,但我想知道是否有更简洁的方法来做到这一点。另外,如果值在任何一步都为空,我该如何跳出链条?
axios.post('/api/login', accounts)
.then((response) => {
this.nonce = response.data
return this.nonce
}).then((nonce) => {
let signature = this.signing(nonce)
return signature
}).then((signature) => {
this.verif(signature)
})
.catch((errors) => {
...
})
解决方案
你通过抛出一个错误打破了承诺链:
axios.post('/api/login', accounts)
.then((response) => {
this.nonce = response.data
return this.nonce
}).then((nonce) => {
if (!nonce) throw ("no nonce")
let signature = this.signing(nonce)
return signature
}).then((signature) => {
if (!signature) throw ("no signature")
this.verif(signature)
})
.catch((errors) => {
...
})
推荐阅读
- javascript - Ndv3 图表应通过单击重绘子图表和子图表
- java - 添加列表项时JavaFX TableView行不刷新
- protractor - 量角器启动 chrome 数据:。url 并且需要将近 10 分钟才能导航到应用程序 URL
- r - 计算 data.table 中事件以来的天数
- python - 使用 scikit-learn 训练模型时出现问题,shell 重新启动
- php - PHP 选择和内部连接表问题。
- javascript - Grunt babel 没有为 IE 转换箭头函数
- json - 如何将 JSON 数据附加到现有 JSON 文件 node.js
- java - 尽管存在密钥,为什么捆绑始终为空?
- javascript - 尝试使用 mkdirp 创建目录时,Node.js 引发错误