Consider a node.js loop to fire off http requests, like this:
var http = require('http');
function sendOne(opts, body)
{
var post_options = {
...
}
var sender = http.request(post_options, function(res) {
// process response
});
sender.write(body)
sender.end()
return sender;
}
for( var i =0; i < maxUploadIterations; i++)
{
var body = // construct payload
var sent = sendOne(opts, body);
// somehow wait on sent...
}
Note that the http.request object has a callback function specified for handling the response. My question is how do I synchronously wait on the "Sent" object returned from sendOne using the built in Node primitives.
I understand there are multiple frameworks like Express and Futures that can handle this, but I want to understand the primitive behavior rather than using a "magic" framework.
Is it possible to take "Sent" and put a handler on it like this:
var sent = ...
sent.on("complete", function() { ... } );
?
If so, exactly which handler and how to format the loop with it?