async.each(arr, function(item,callback) {
},
function(err) {
//function code that is to be done after the 2nd
//argument of async.each is called on each element
})
- 1st parameter: array to do function on (for every element)
- 2nd parameter: function that you are calling for each thing in the array
- this function usually has 2 parameters:
- a reference to the current element
- the callback function you should call indicating that the function has executed for the current element and is done)
- this function usually has 2 parameters:
- 3rd parameter: function called when the previous function has completed for all function elements. This can take in an error as an argument (will be null if no errors)
Checkout these seed files that use async.each()
:
https://github.com/tmoreton/stackStore/blob/master/seed.js
https://github.com/seemaullal/coduels/blob/master/seed.js
async.map(arr, function(item,callback) {
//function called on each item of arr
//when done, call callback(error, result) where error=null if no error
},
function(err, results) {
//function called after 2nd argument of async.map called on each element in arr
//results is an array of the results. err will be null if no errors passed
})
- 1st parameter: array to do function on (for every element)
- 2nd parameter: function that you are calling for each thing in the array
- this function usually has 2 parameters:
- a reference to the current element
- the callback function you should call indicating that the function has executed for the current element and is done).
- When calling the callback, you need 2 arguments: an error (null if there is no error) and the result that you want in the array async.map returns
- this function usually has 2 parameters:
- 3rd parameter: function called when the previous function has completed for all function elements. This should have 2 arguments: an error (null if no errors) and results (an array of all the results retuurned in the callback function for each element)
var timesTwo = function (n, done) {
return done(null, n * 2);
};
async.map([1, 2, 3, 4], timesTwo, function (err, results) {
/* Note you can pass an actual function or an anonymmous function for the funciton arguments
since timesTwo has completed for each element
(we know because we are in the 3rd arg function, we can return our results */
console.log('All done.');
console.log(results); // will output [2,4,6,8] but the original array won't be changed
});
async.parallel( [/*array of async functions to execute */ ], function(err, results) {
// this will be called after every function in the array is completed
//results is an array of any passed results. err will be null if no errors
})
-
1st parameter: array that has asynchronous functions in it. these will execute in parallel. Each should have a callback function as a parameter (you call that function when the current function is done so the async library knows you are done with that function
- this callback function is called indicating that the function has executed and is done. This callback function can take in an error (1st arg) and result (2nd arg) that will be saved
- Note: if you want to pass a result to a callback you need to make the first parameter null indicating that there is no error)
-
2nd parameter: function that is executed after all the functions in the previous array have completed. This function can have 2 optional arguments - an error (1st arg; null if no errors) and a results array (all the results passed into the 2nd parameter of the previous callbacks.