Skip to content

Instantly share code, notes, and snippets.

@morganherlocker
Last active August 29, 2015 14:07
Show Gist options
  • Select an option

  • Save morganherlocker/ea64d973bd5fc196323a to your computer and use it in GitHub Desktop.

Select an option

Save morganherlocker/ea64d973bd5fc196323a to your computer and use it in GitHub Desktop.
async control flow

When you make an async request to a server, you fire it off and wait for the server to respond. The server could respond to multiple requests in any order it likes. If you are working on something that needs to be in order, there are a few things that you can do to control the flow.

counting

The most basic approach involves counting up responses outside of callbacks until the number of responses === the number of requests. This works, but can get crazy if you have a lot of complex events flying around everywhere.

var urls = [...]
var pages = []
for(var i = 0; i < urls.length; i++) {
	request(url, function(err, data){
		pages.push(data)
		if(pages.length === urls.length) doStuff(pages)
	})
}

function doStuff(pages);

Using Libraries

There are loads of async control flow libraries. The most popular one is probably async.js.

Using async, you can mimic a sync flow with callbacks using the sync function:

async.series([
    function(){ ... },
    function(){ ... }
]);

A simpler library is queue-async. With queue-async, you load up a bunch of tasks, then fire them all off at once. It returns an array of results in a single callback.

// create a queue with parallelism set to 1; this can be set higher to run more tasks at once.
var q = queue(1);
// give it a bunch of stuff to do
tasks.forEach(function(t) { 
  q.defer(t); 
});
// process the results when everything has completed
q.awaitAll(function(error, results) { console.log("all done!"); });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment