Created
June 21, 2014 09:57
-
-
Save joesavage/8216542605fd6ad45677 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.