我有以下的承诺。在每一步中,我都需要计算返回的值是否为null。我可以在每一步中添加一个if there条件,但我想知道是否有一种更简洁的方法来做到这一点。此外,如果值在任何步骤为null,我如何才能脱离链呢?
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) => {
...
})发布于 2020-08-03 13:49:57
你打破了承诺链,抛出了一个错误:
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) => {
...
})发布于 2020-08-03 13:40:35
嵌套承诺是不必要的。尝尝这个
axios.post('/api/login', accounts)
.then(async (response) => {
this.nonce = response.data
let signature = await this.signing(this.nonce);
if(!signature){
throw "invalid"
}
this.verif(signature);
.catch((errors) => {
...
})发布于 2020-08-03 13:59:11
简洁,它很可能是一个.then(),对于检查,可以抛出任何空值。
axios.post('/api/login', accounts)
.then(async (response) => {
if(!response.data) throw "Response Error"
this.nonce = response.data
const signature = await this.signing(this.nonce);
if(!signature) throw "invalid"
this.verif(signature)
})
.catch((errors) => {
...
})https://stackoverflow.com/questions/63230335
复制相似问题