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!