Last active
August 29, 2015 14:06
-
-
Save bradoyler/3584fa7ba29b33289028 to your computer and use it in GitHub Desktop.
using async.parallel vs async.each
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
async = require("async"); | |
var results = []; | |
async.each(states, | |
function(state, callback){ | |
var url = baseUrl + state.name; | |
request(url, function(err, res, body){ | |
results.push(data); | |
callback(); | |
}); | |
}, | |
function(err){ | |
//done, now save results. | |
store.save(results); | |
} | |
); |
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
async = require("async"); | |
var asyncTasks = []; | |
var results = []; | |
states.forEach(function(state){ | |
asyncTasks.push(function(callback){ | |
var url = baseUrl + state.name; | |
request(url, function(err, res, body){ | |
results.push(data); | |
callback(); | |
}); | |
}); | |
}); | |
// Execute tasks simultaneously | |
async.parallel(asyncTasks, function(){ | |
//done, now save results. | |
store.save(results); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment