Skip to content

Instantly share code, notes, and snippets.

@seemaullal
Last active July 27, 2017 03:03
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 seemaullal/3f5ef89bd9e62fbafaa5 to your computer and use it in GitHub Desktop.
Save seemaullal/3f5ef89bd9e62fbafaa5 to your computer and use it in GitHub Desktop.
Async Library Notes

Some Useful Async library methods

async.each

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)
  • 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)

Example

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

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
  • 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)

Example

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

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.

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