0

I am using Q in a Node.js application to implement promises and have something like this in which there is some promise chaining:

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

In the second then I need to use result1 from the previous then block but it isn't available. Is there a way to access it?

NOTE: There are similar questions but none of them solve the problem for Q library.

codependent
  • 23,193
  • 31
  • 166
  • 308

1 Answers1

1

Chain off of your inner promise:

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.
  });
});

This is likely the best way to do it given that getting result2 requires having result1 already. If they didn't depend on each other like that, you could use Promise.all.

CRice
  • 29,968
  • 4
  • 57
  • 70