繁体   English   中英

如何在中间突破承诺链

[英]How to break out of the promise chain in the middle

这是我的示例代码。

Orchestrator首先用几个输入呼叫工作人员,获得响应后,必须验证响应是否令人满意。

如果满意,请返回给呼叫者。

如果不是,请再次致电相同的工作人员,也可以是其他工作人员,输入稍有不同,然后按照流程进行操作。

在这里,尽管我的代码在第一个工作程序调用之后调用cb(),但也将在第二个工作程序调用之后调用,并且未定义“响应”错误。

我可以添加一个额外的条件来检查第一个响应是否令人满意,例如第二个中的need2ndworkercall && validate(response)并摆脱它。 但是想知道什么是解决这个问题的正确方法。 感谢任何反馈。

  function orchestrateSomething(input, cb){
    doSomething(input.a, input.b)
      .then(response=>{
        if(validate(response)){
          cb(buildResultObj(response));
        }
        else{
          return doSomething(input.a)
        }
      })
      .then(response=>{
        if(validate(response)){
          cb(buildResultObj(response));
        }
        else{
          cb(null,{});
        }
      })
      .catch(error=>cb(error));
  }

从函数和.then() return值。 此外, cb函数应调用传递的函数,该函数返回值或评估参数并返回传递的值

  function orchestrateSomething(input, cb){
    return doSomething(input.a, input.b)
      .then(response=>{
        if(validate(response)){
          return cb(buildResultObj(response));
        }
        else{
          return doSomething(input.a)
        }
      })
      .then(response=>{
        if(validate(response)){
          return cb(buildResultObj(response));
        }
        else{
          return cb(null,{});
        }
      })
      .catch(error=>cb(error));
  }

  orchestrateSomething(input, cb) // where `cb` calls function or values passed
  .then(function(results) {
    console.log(results)
  })
  .catch(function(err) {
    console.log(err)
  });

可以通过简单的throw打破承诺链。 诀窍是在catch调用中正确处理它:

doPromise(...)
  .then(...)
  .then(result => {
    if(condition) {
      throw result
    }
    else {
      return doPromise()
    }
  })
  .then(...)
  .catch(result => {
    if(result instanceof Error) {
      // handle error result
    }
    else {
      // handle desired result
    }
  })

这是这种方法的最简单演示: http ://plnkr.co/edit/H7K5UsZIueUY5LdTZH2S?p=preview

顺便说一句,如果你能概括then处理功能,可以进行递归调用:

processCB = (result) => {
  if(condition) {
    throw result
  }
  else {
    return doPromise()
  }
}

catchCB = (result) => {
  if(result instanceof Error) {
    // handle error result
  }
  else {
    // handle desired result
  }
}

doProcess = () => doPromise()
  .then(processCB)
  .catch(catchCB)

这是第二部分的演示: http : //plnkr.co/edit/DF28KgBOHnjopPaQtjPl?p=preview

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM