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

Javascript 评估连锁承诺的每一步的价值,并打破承诺,javascript,node.js,vue.js,Javascript,Node.js,Vue.js,我有以下承诺。在每个步骤中,我都需要评估返回值是否不为null。我可以在每个步骤中添加if-else条件,但我想知道是否有更简洁的方法来实现这一点。此外,如果在任何步骤中该值为null,如何断开该链 axios.post('/api/login', accounts) .then((response) => { this.nonce = response.data return this.nonce })

我有以下承诺。在每个步骤中,我都需要评估返回值是否不为null。我可以在每个步骤中添加if-else条件,但我想知道是否有更简洁的方法来实现这一点。此外,如果在任何步骤中该值为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) => {
          ...
        })

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

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) => {
          ...
        })

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

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) => {
          ...
        })

您通过抛出一个错误来打破承诺链:

       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) => {
          ...
        })

您通过抛出一个错误来打破承诺链:

       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) => {
          ...
        })

简洁性它可能是一个。然后,对于检查,抛出任何空值

axios.post'/api/login',帐户 .thenasync响应=>{ if!response.data抛出响应错误 this.nonce=response.data const signature=等待this.signingthis.nonce; 如果!签名无效 这是我的签名 } .catcherrors=>{ ... }
简洁性它可能是一个。然后,对于检查,抛出任何空值

axios.post'/api/login',帐户 .thenasync响应=>{ if!response.data抛出响应错误 this.nonce=response.data const signature=等待this.signingthis.nonce; 如果!签名无效 这是我的签名 } .catcherrors=>{ ... }
谢谢你的回答。谢谢你的回答。谢谢你的回答。雅各布。没问题!如果这不起作用,请告诉我,我相信我们能解决它!:谢谢你的回答,雅各布,没问题!如果这不起作用,请告诉我,我相信我们能解决它!: