Asynchronous

APIUtilsAsynchronous

Access it with:

var utils = require('utils');
var async = utils.async;

Source

utils.async.forEach(array, callback, onEnd, context)

Parameters:
  • array — NotPrimitive
  • callback — Function
  • onEnd — Functionoptional
  • context — Anyoptional

This is an asynchronous version of the standard Array.prototype.forEach() function which works with arrays and objects.

The given callback function is called with parameters:

var toLoadInOrder = ['users.json', 'families.js', 'relationships.js'];

utils.async.forEach(toLoadInOrder, function(elem, i, array, next){
  console.log("Load " + elem + " file");
  // on load end ...
  next();
}, function(){
  console.log("All files are loaded!");
});

// Load users.json
// Load families.json
// load relationships.json
// All files are loaded!

Source

**Class Stack**()

Stores functions and runs them synchronously or asynchronously.

var stack = new utils.async.Stack;

function load(src, callback){
  console.log("Load " + src + " file");
  // load file async ...
  // first callback parameter is an error ...
  callback(null, "fiel data");
};

stack.add(load, null, ['items.json']);
stack.add(load, null, ['users.json']);

stack.runAllSimultaneously(function(){
  console.log("All files have been loaded!");
});

// Load items.json file
// Load users.json file
// All files have been loaded!

// or ... (simultaneous call has no order)

// Load users.json file
// Load items.json file
// All files have been loaded!

Source

Stack::add(function, context, arguments)

Parameters:
  • function — Function
  • context — Anyoptional
  • arguments — NotPrimitiveoptional

Adds the given function to the stack.

The function must provide a callback argument as the last argument. The first argument of the callback function is always an error.

var stack = new utils.async.Stack;

function add(a, b, callback){
  if (isFinite(a) && isFinite(b)){
    callback(null, a+b);
  } else {
    throw "Finite numbers are required!";
  }
}

stack.add(add, null, [1, 2]);

stack.runAll(function(err, result){
  console.log(err, result);
});
// null 3

stack.add(add, null, [1, NaN]);

stack.runAll(function(err, result){
  console.log(err, result);
});
// "Finite numbers are required!"  undefined

Source

Stack::callNext(arguments, callback)

Parameters:
  • arguments — Arrayoptional
  • callback — Function

Calls the first function from the stack and remove it.

Source

Stack::runAll(callback, callbackContext)

Parameters:
  • callback — Functionoptional
  • callbackContext — Anyoptional

Calls all functions from the stack one by one.

When an error occurs, processing stops and the callback function is called with the got error.

Source

Stack::runAllSimultaneously(callback, callbackContext)

Parameters:
  • callback — Functionoptional
  • callbackContext — Anyoptional

Calls all functions from the stack simultaneously (all at the same time).

When an error occurs, processing stops and the callback function is called with the got error.

Source