Node.js - Q 库,我如何访问在之前的 then() 中获得的值

Node.js - Q library, how can I access the value gotten in a previous then()

我在 Node.js 应用程序中使用 Q 来实现承诺,并且有类似这样的东西,其中有一些承诺链接:

service.execute()
.then((result1) => {
  return service2.execute(result1);
})
.then((result2) => {
  //Here I need result1 and result2!!!
});

在第二个 then 中,我需要使用前一个 then 块中的 result1,但它不可用。有没有办法访问它?

注意:有类似的问题,但其中 none 解决了 Q 库的问题。

实现你内心的承诺:

service.execute().then((result1) => {
  return service2.execute(result1).then((result2) => {
      // Here I have access to both result1 and result2.
      // Result1 via the closure, Result2 as an argument.
  });
});

考虑到获得 result2 需要已经拥有 result1,这可能是最好的方法。如果他们不像那样相互依赖,你可以使用 Promise.all