I am experiencing what seems to be a traditional problem for Node JS beginners, and async requests.
I have an unknown number of URLs generate by the user, and subsequently stored in an array on my Node JS server. The Node JS server must iterate through these URLs, making a request to each one in turn. It must do so in order, and it must wait for each response before moving onto the next URL (when a new request will be made). The final result should be the in-order collection of all the responses (which happen to be JSON), nicely stored together as a JSON object, which in turn can be sent back to the client when ready.
I think I should use the async
NodeJS library, and I am already using needle
for making the requests.
URLs = ["http://a", "http://s", "http://d"];
async.eachSeries(URLs, function (URL, callback) { ..... });
I'm not clear how to use async to ensure that the Needle request has finished, and store that response accordingly before moving onto the next URL request. Below is an example of my Needle request.
needle.get(URL, options, function(error, response, body){ ... });
Either a partial or complete solution to the whole problem is welcome.