4

I'm trying to chain a series of GET requests together. They're a series of API calls which depend on data from previous calls. My understanding of promises is that I should be able to make a flat .then() chain, but when I was trying to do this, my functions/console.logs were not executing in the proper order, so I presently have a growing pyramid of doom:

var request = require('request');
var deferredGet = Q.nfbind(request);

deferredGet(*params*)
  .then(function(response){
  // process data from body and add it to the userAccount object which I am modifying.
    return userAccount;
  })
  .then(function(userAccount){
    deferredGet(*params*)
      .then(function(response){
        //process data from body and add to userAccount
        return userAccount;
    })
    .then(function..... // There's a total of 7 API calls I need to chain, and it's already getting unwieldy.

I understand that you are supposed to return a promise, se perhaps I should be returning deferredGet, but when I tried to do that I was not returning anything. Also, the parameter passed into the first then is the response, not a promise. So I don't know where to go from here, but I feel like I'm doing it wrong.

Thanks in advance!

ChadF
  • 1,750
  • 10
  • 22
  • This is definitely **not** a duplicate of the question linked by Benjamin Gruenbaum, though it is relevant and might be helpful. – CatDadCode Apr 16 '15 at 19:34
  • This _is_ a duplicate he's asking the exact same thing I meant when I asked the question there and is missing the same point. I gave your (good) answer an upvote and you're welcome to add an answer there - but this chaining ability is what both questions are about. – Benjamin Gruenbaum Apr 16 '15 at 20:53
  • Chad: what version of node/io.js are you using? – Benjamin Gruenbaum Apr 16 '15 at 20:59
  • I got it up and running, I had a fundamental misunderstanding of how promises function that the more detailed examples helped with. Much thanks to both you and Alex for your time! – ChadF Apr 16 '15 at 21:44

1 Answers1

3

You're correct that you should be returning deferredGet. However, realize that what is returned is still a promise. So you should keep chaining .then calls afterward.

var request = require('request');
var deferredGet = Q.nfbind(request);

deferredGet(*params*)
  .then(function(response){
    // process data from body and add it to the userAccount object which I am modifying.
    return userAccount;
  })
  .then(function(userAccount){
    return deferredGet(*params*);
  })
  .then(function(response){
    // response should be the resolved value of the promise returned in the handler above.
    return userAccount;
  })
  .then(function (userAccount) {
    //...
  });

When you return a promise from within a then handler, Q will make it part of the chain. If you return a raw value from a handler, Q will make an implied promise that simply immediately resolves with that raw value, as you saw with userAccount in the first handler.

Check out this working example I put together for you :)

CatDadCode
  • 58,507
  • 61
  • 212
  • 318
  • 1
    Thank you, this helped a lot. I follow the idea of promises, I just needed a more fleshed out example. My code is now up and running and a whole lot less ugly! – ChadF Apr 16 '15 at 21:07