Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How to get serial flow control of multiple asynchronous http requests and their callbacks in node.js?

How to get serial flow control of multiple asynchronous http requests and their callbacks in node.js?

Problem

I have tried this many ways (queue, with/without async module, handler, etc.), but I cannot figure out how to apply serial flow control to an asynchronous http request and its Callback. I have an array of urls I want to use to make http requests with and insert individual documents within each response into a mongo db collection. Once the inserts are finished I want to proceed to the next http request. Here is where I am at, but this still kicks off all the http requests before the inserts happen.

var request = require('request');
var async = require('async');

var urls = ['http://getsomejson/1', 'http://getsomejson/2', 'http://getsomejson/3'];

async.forEachSeries(urls, function(url, callback) {
    // All of these requests are firing before http request callback logic is executed
    request(url, function (error, response, body) {
        async.forEachSeries(body.docs, function(doc, callback) {
            // Do the inserts for this response
            callback();
        }, function(err) {
            // handle errors
        });
    })
    callback();
}, function(err) {
    // handle errors
});

Any suggestions would be greatly appreciated.

Problem courtesy of: TankofVines

Solution

The callback passed to the function(url, callback) function is what triggers the next element in the series. If you just call it at the end like that, it is doing a loop just like if you did a for loop. You should call that callback in the completion function of the request. That way it will jump to the next item after the request is done.

async.forEachSeries(urls, function(url, callback) {
  // All of these requests are firing before http request callback logic is executed
  request(url, function (error, response, body) {

    async.forEachSeries(body.docs, function(doc, callback) {
      // Do the inserts for this response
      callback();
    }, function(err) {
      // handle errors

      callback();
    });
  })
}, function(err) {
  // handle errors
});
Solution courtesy of: loganfsmyth

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

How to get serial flow control of multiple asynchronous http requests and their callbacks in node.js?

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×