-
-
Save Goddard/538eb857228a79c9ccdb50f6cf0fad17 to your computer and use it in GitHub Desktop.
Multiple Requests with Request (Node.js)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var request = require('request') | |
/** | |
* Handle multiple requests at once | |
* @param urls [array] | |
* @param callback [function] | |
* @requires request module for node ( https://github.com/mikeal/request ) | |
*/ | |
var __request = function (urls, callback) { | |
'use strict'; | |
var results = {}, t = urls.length, c = 0, | |
handler = function (error, response, body) { | |
var url = response.request.uri.href; | |
results[url] = { error: error, response: response, body: body }; | |
if (++c === urls.length) { callback(results); } | |
}; | |
while (t--) { request(urls[t], handler); } | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* How to use __request() | |
* step by step | |
*/ | |
// create an array of URLs | |
var urls = ["http://www.example.com/firts", "http://www.example.com/second", "http://www.example.com/third"]; | |
// execute the request | |
// and assign a callback | |
__request(urls, function(responses) { | |
// When all the requests are ready | |
// this callback will be called | |
// you will get an argument, is | |
// a map with all the responses | |
// of the request you made, | |
// something like this: | |
/* | |
responses = { | |
"http://www.example.com/firts": { | |
error: [object Object], | |
response: [object Object], | |
body: [object Object] | |
}, | |
"http://www.example.com/second": { | |
error: [object Object], | |
response: [object Object], | |
body: [object Object] | |
}, | |
"http://www.example.com/third": { | |
error: [object Object], | |
response: [object Object], | |
body: [object Object] | |
} | |
} | |
*/ | |
// Acces to a response: | |
// direct reference | |
var first_response = response["http://www.example.com/first"]; | |
// check for errors of the first response | |
console.log(first_response.error); | |
// access to the body of the first response | |
console.log(first_response.body); | |
// also you can reuse the reference on the original array | |
var first_response = response[urls[0]]; | |
// Iterate responses: | |
// You can simply iterate all responses | |
// to find errors or process the response | |
var url, response; | |
for (url in responses) { | |
// reference to the response object | |
response = responses[url]; | |
// find errors | |
if (response.error) { | |
console.log("Error", url, response.error); | |
return; | |
} | |
// render body | |
if (response.body) { | |
console.log("Render", url, response.body); | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment