首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在连锁承诺的每一步评估价值,并打破承诺

在连锁承诺的每一步评估价值,并打破承诺
EN

Stack Overflow用户
提问于 2020-08-03 13:26:58
回答 3查看 42关注 0票数 1

我有以下的承诺。在每一步中,我都需要计算返回的值是否为null。我可以在每一步中添加一个if there条件,但我想知道是否有一种更简洁的方法来做到这一点。此外,如果值在任何步骤为null,我如何才能脱离链呢?

代码语言:javascript
复制
       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) => {
          ...
        })
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2020-08-03 13:49:57

你打破了承诺链,抛出了一个错误:

代码语言:javascript
复制
       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) => {
          ...
        })
票数 3
EN

Stack Overflow用户

发布于 2020-08-03 13:40:35

嵌套承诺是不必要的。尝尝这个

代码语言:javascript
复制
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) => {
          ...
        })
票数 2
EN

Stack Overflow用户

发布于 2020-08-03 13:59:11

简洁,它很可能是一个.then(),对于检查,可以抛出任何空值。

代码语言:javascript
复制
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) => {
          ...
        })
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63230335

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档