代码之家  ›  专栏  ›  技术社区  ›  I'm Joe Too

什么时候需要返回嵌套的承诺?

  •  0
  • I'm Joe Too  · 技术社区  · 7 年前

    当链接多个 then 语句,我很难理解何时需要将值返回给下一个 语句与自动传递时的语句。(对我来说)困惑的是,当我在一家公司里有一个承诺时 然后 声明与否。

    res.send() 有些价值。

    // Do I need to return mainFunction()?
    mainFunction()
      .then(resultOfMyFunction => {
        // I want the next "then" to wait for the response from this block
        // Do I have to return asyncFunction() or just the value below?
        asyncFunction().then(resultOfPromise => {
          // Do I return resultOfPromise?
        }).catch(error => {
          // If I return this error, will it go to the mainFunction catch block?
          return error
        })
      }).then(resultOfPromise => {
        // This is blocking, so the next "then" should wait for the value
        return synchronousFunction(resultOfPromise)
      }).then(resultOfSynchronousFunction => {
        // End of function - do I need to return resultOfSynchronousFunction?
      }).catch(error => {
        // Do I need to return error?
      })
    

    3 回复  |  直到 7 年前
        1
  •  -1
  •   Liam Joshua    7 年前

    我想你想要这样的东西。我是这么想的 This is blocking 你是说 synchronousFunction 不是异步的。在这种情况下,它不需要以承诺式的方式消费。

    mainFunction()
      .then(resultOfMyFunction => {
        //return promise from async function
        return asyncFunction();
      })
      .then(resultOfPromise => {
        let resultOfSynchronousFunction = synchronousFunction(resultOfPromise);
        // End of function
      })
      .catch(error => {
        // Do I need to return error?
      });
    

    resultOfSynchronousFunction How do I return the response from an asynchronous call?

    如果要返回的异步版本 同步功能

    mainFunction()
      .then(resultOfMyFunction => {
        //return promise from async function
        return asyncFunction();
      })
      .then(resultOfPromise => {
        return new Promise((resolve, reject) => {
           let resultOfSynchronousFunction = synchronousFunction(resultOfPromise);
           // End of function
           resolve(resultOfSynchronousFunction);
        });
      })
      .then(resultOfSynchronousFunction => {})
      .catch(error => {
        // Do I need to return error?
      });
    

    如果要在之后运行另一个异步调用 更好的模式是:

    mainFunction()
      .then(resultOfMyFunction => {
        //return promise from async function
        return asyncFunction();
      })
      .then(resultOfPromise => {
    
           let resultOfSynchronousFunction = synchronousFunction(resultOfPromise);
           // End of function
           return anotherAsyncCall(resultOfSynchronousFunction );
      })
      .then(resultOfanotherAsyncCall => {})
      .catch(error => {
        // Do I need to return error?
      });
    
        2
  •  2
  •   Benjamin Gruenbaum    7 年前

    每当你在一个房间里有一个承诺 then 或者一个异步函数

    同步函数没有这样的限制-只有当您想重用同步代码的返回值时,返回同步代码才重要,因为抛出的错误由 自动地

      // If I return this error, will it go to the mainFunction catch block?
    

    承诺真的很不神奇。返回值是错误/成功(拒绝/完成)状态在调用之间传播的方式,因此如果不返回值,main函数将不会转到其catch块。

    如果您想让它转到catch块,那么返回值必须是最终拒绝的承诺,因此您需要在内部 .catch .接住 从那里开始。

        3
  •  0
  •   charlietfl    7 年前

    前两个函数中并不需要匿名函数 then()

    可以通过将函数引用传递给 相反

    mainFunction()
      .then(asyncFunction)
      .then(synchronousFunction)
      .then(resultOfSynchronousFunction => {
        // consume resultOfSynchronousFunction or return to next then()
      }).catch(error => {
        // Do I need to return error?
        // only if this is not final catch block
      })