Skip to content

Instantly share code, notes, and snippets.

@joesavage
Created June 21, 2014 09:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joesavage/8216542605fd6ad45677 to your computer and use it in GitHub Desktop.
Save joesavage/8216542605fd6ad45677 to your computer and use it in GitHub Desktop.
var results = {}; // Stores results of async calculations
var render = function() { /* ... render the page using 'results' ... */};
var getDatabaseRecords = function(callback) { /* ... async stuff, storing results in 'results' ... */ };
var prepareAssetPaths = function(callback) { /* ... async stuff, storing results in 'results' ... */ };
var tasks = [getDatabaseRecords, prepareAssetPaths],
params = [
[],
[['stylesheet.scss', 'image.png', 'script.js']]
],
times = tasks.length;
tasks.forEach(function(task, index) {
var _params = params[index] || [];
_params.push(function() { if(--times === 0) render(); });
task.apply(this, _params);
});
@joesavage
Copy link
Author

The async node module makes this kind of behaviour much easier to get at using async.parallel, plus provides a bunch of other useful stuff.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment