Link and share previous results with Promises

I am using the bluebird library and you need to make a series of HTTP requests, and some response data will be passed to the next HTTP request. I created a function that processes my requests called callhttp() . It takes up the URL and the body of the POST.

I call it this way:

 var payload = '{"Username": "joe", "Password": "password"}'; var join = Promise.join; join( callhttp("172.16.28.200", payload), callhttp("172.16.28.200", payload), callhttp("172.16.28.200", payload), function (first, second, third) { console.log([first, second, third]); }); 

The first request receives an API key, which must be passed to the second request, and so on. How to get response data from the first request?

UPDATE

This is the callhttp function:

 var Promise = require("bluebird"); var Request = Promise.promisify(require('request')); function callhttp(host, body) { var options = { url: 'https://' + host + '/api/authorize', method: "POST", headers: { 'content-type': 'application/json' }, body: body, strictSSL: false }; return Request(options).spread(function (response) { if (response.statusCode == 200) { // console.log(body) console.log(response.connection.getPeerCertificate().subject.CN) return { data: response.body }; } else { // Just an example, 200 is not the only successful code throw new Error("HTTP Error: " + response.statusCode ); } }); } 
+32
javascript bluebird
Feb 25 '15 at 8:25
source share
1 answer

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) { // multiple results available in results array // that can be processed further here with // other promises }); 
+82
Feb 25 '15 at 8:58
source share



All Articles