There are several models for dependent promises and data transfer from one to another. Which one works best depends on whether you only need previous data in the next call or if you need access to all previous data. Here are some models:
Result from one to the next
callhttp(url1, data1).then(function(result1) { // result1 is available here return callhttp(url2, data2); }).then(function(result2) { // only result2 is available here return callhttp(url3, data3); }).then(function(result3) { // all three are done now, final result is in result3 });
Assign intermediate results to a higher scale
var r1, r2, r3; callhttp(url1, data1).then(function(result1) { r1 = result1; return callhttp(url2, data2); }).then(function(result2) { r2 = result2; // can access r1 or r2 return callhttp(url3, data3); }).then(function(result3) { r3 = result3; // can access r1 or r2 or r3 });
Accumulation of results in one object
var results = {}; callhttp(url1, data1).then(function(result1) { results.result1 = result1; return callhttp(url2, data2); }).then(function(result2) { results.result2 = result2; // can access results.result1 or results.result2 return callhttp(url3, data3); }).then(function(result3) { results.result3 = result3; // can access results.result1 or results.result2 or results.result3 });
Socket so all previous results may be available
callhttp(url1, data1).then(function(result1) { // result1 is available here return callhttp(url2, data2).then(function(result2) { // result1 and result2 available here return callhttp(url3, data3).then(function(result3) { // result1, result2 and result3 available here }); }); })
Break the chain into independent parts, collect the results
If some parts of the chain can work independently of each other, and not one after another, you can run them separately and use Promise.all() to know when these several parts will be executed, and then you will have all the data from these independent parts:
var p1 = callhttp(url1, data1); var p2 = callhttp(url2, data2).then(function(result2) { return someAsync(result2); }).then(function(result2a) { return someOtherAsync(result2a); }); var p3 = callhttp(url3, data3).then(function(result3) { return someAsync(result3); }); Promise.all([p1, p2, p3]).then(function(results) {