Socket
Socket
Sign inDemoInstall

neo-async

Package Overview
Dependencies
0
Maintainers
1
Versions
77
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    neo-async

Neo-Async is compatible with Async.js, it is faster and has more feature.


Version published
Maintainers
1
Install size
221 kB
Created

Package description

What is neo-async?

The neo-async package is a utility module which provides straight-forward, powerful functions for working with asynchronous JavaScript. It is similar to the async package but with some performance improvements.

What are neo-async's main functionalities?

Control Flow

Execute a series of functions in sequential order. Each function is passed a callback it must call on completion.

async.series([
  function(callback) {
    // do some stuff ...
    callback(null, 'one');
  },
  function(callback) {
    // do some more stuff ...
    callback(null, 'two');
  }
],
function(err, results) {
  // results is now equal to ['one', 'two']
});

Collections

Apply a function to each item in a collection and collect the results.

async.map(['file1','file2','file3'], fs.stat, function(err, results) {
  // results is now an array of stats for each file
});

Utilities

Call a function a certain number of times and collect the results.

async.times(5, function(n, next) {
  createUser(n, function(err, user) {
    next(err, user);
  });
}, function(err, users) {
  // we should now have 5 users
});

Other packages similar to neo-async

Readme

Source

Neo-Async

Build Status codecov.io

Neo-Async

nodei

Neo-Async is compatible with Async.js, it is faster and has more feature.

Async is a utility module which provides staright-forward.

jsperf Comparison


Installation

In a browser

<script src="async.min.js"></script>

In an AMD loader

require(['async'], function(async) {});

Node.js

standard
$ npm install neo-async
var async = require('neo-async');
replacement
$ npm install neo-async
$ ln -s ./node_modules/neo-async ./node_modules/async
var async = require('async');

Feature

Collections

Control Flow

Utils


### concat(collection, iterator, [callback], [thisArg])

Arguments

  1. collection (Array|Object): The collection to iterate over.
  2. iterator(item, callback) (Function): The function called per iteration.
  3. callback(err, array) (Function): The function called at the end.
  4. thisArg (*): The this binding of iterator.
var order = [];
var collection = [1, 3, 2];
var iterator = function(num, done) {
  setTimeout(function() {
    order.push(num);
    done(null, num);
  }, num * 10);
};
async.concat(collection, iterator, function(err, array) {
  assert.deepEqual(array, [1, 2, 3]);
  assert.deepEqual(order, [1, 2, 3]);
});


### concatSeries(collection, iterator, [callback], [thisArg])

Arguments

  1. collection (Array|Object): The collection to iterate over.
  2. iterator(item, callback) (Function): The function called per iteration.
  3. callback(err, array) (Function): The function called at the end.
  4. thisArg (*): The this binding of iterator.
var order = [];
var collection = [1, 3, 2];
var iterator = function(num, done) {
  setTimeout(function() {
    order.push(num);
    done(null, num);
  }, num * 10);
};
async.concatSeries(collection, iterator, function(err, array) {
  assert.deepEqual(array, [1, 3, 2]);
  assert.deepEqual(order, [1, 3, 2]);
});


### concatLimit(collection, limit, iterator, [callback], [thisArg])

Arguments

  1. collection (Array|Object): The collection to iterate over.
  2. limit (Number): The maximum number of iterators to run at any time.
  3. iterator(item, callback) (Function): The function called per iteration.
  4. callback(err, array) (Function): The function called at the end.
  5. thisArg (*): The this binding of iterator.
var order = [];
var collection = [1, 3, 2];
var iterator = function(num, done) {
  setTimeout(function() {
    order.push(num);
    done(null, num);
  }, num * 10);
};
async.concatLimit(collection, 2, iterator, function(err, array) {
  assert.deepEqual(array, [1, 3, 2]);
  assert.deepEqual(order, [1, 3, 2]);
});


### detect(collection, iterator, [callback], [thisArg])

Arguments

  1. collection (Array|Object): The collection to iterate over.
  2. iterator(item, callback) (Function): The function called per iteration.
  3. callback(item) (Function): The function called at the end.
  4. thisArg (*): The this binding of iterator.
var order = [];
var collection = [1, 3, 2];
var iterator = function(num, done) {
  setTimeout(function() {
    order.push(num);
    done(num === 3);
  }, num * 10);
};
async.detect(collection, iterator, function(item) {
  assert.deepEqual(item, 3);
  assert.deepEqual(order, [1, 2, 3]);
});


### detectSeries(collection, iterator, [callback], [thisArg])

Arguments

  1. collection (Array|Object): The collection to iterate over.
  2. iterator(item, callback) (Function): The function called per iteration.
  3. callback(item) (Function): The function called at the end.
  4. thisArg (*): The this binding of iterator.
var order = [];
var collection = [1, 3, 2];
var iterator = function(num, done) {
  setTimeout(function() {
    order.push(num);
    done(num === 3);
  }, num * 10);
};
async.detectSeries(collection, iterator, function(item) {
  assert.deepEqual(item, 3);
  assert.deepEqual(order, [1, 3]);
});


### detectLimit(collection, limit, iterator, [callback], [thisArg])

Arguments

  1. collection (Array|Object): The collection to iterate over.
  2. limit (Number): The maximum number of iterators to run at any time.
  3. iterator(item, callback) (Function): The function called per iteration.
  4. callback(item) (Function): The function called at the end.
  5. thisArg (*): The this binding of iterator.
var order = [];
var collection = [1, 3, 2];
var iterator = function(num, done) {
  setTimeout(function() {
    order.push(num);
    done(num === 3);
  }, num * 10);
};
async.detectLimit(collection, 2, iterator, function(item) {
  assert.deepEqual(item, 3);
  assert.deepEqual(order, [1, 3]);
});


### each(collection, iterator, [callback], [thisArg]) Applies the function iterator to each item in collection, in parallel.

Aliases

async.forEach

Arguments

  1. collection (Array|Object): The collection to iterate over.
  2. iterator(item, callback) (Function): The function called per iteration.
  3. callback(err) (Function): The function called at the end.
  4. thisArg (*): The this binding of iterator.
var order = [];
var collection = [1, 3, 2];
var iterator = function(num, done) {
  setTimeout(function() {
    order.push(num);
    done();
  }, num * 10);
};
async.each(collection, iterator, function(err) {
  assert.deepEqual(order, [1, 2, 3]);
});

### eachSeries(collection, iterator, [callback], [thisArg]) The same as each, in series.

Aliases

async.forEachSeries

Arguments

  1. collection (Array|Object): The collection to iterate over.
  2. iterator(item, callback) (Function): The function called per iteration.
  3. callback(err) (Function): The function called at the end.
  4. thisArg (*): The this binding of iterator.
var order = [];
var collection = [1, 3, 2];
var iterator = function(num, done) {
  setTimeout(function() {
    order.push(num);
    done();
  }, num * 10);
};
async.eachSeries(collection, iterator, function(err) {
  assert.deepEqual(order, [1, 3, 2]);
});

### eachLimit(collection, limit, iterator, [callback], [thisArg]) The same as each, in limited parallel.

Aliases

async.forEachLimit

Arguments

  1. collection (Array|Object): The collection to iterate over.
  2. limit (Number): The maximum number of iterators to run at any time.
  3. iterator(item, callback) (Function): The function called per iteration.
  4. callback(err) (Function): The function called at the end.
  5. thisArg (*): The this binding of iterator.
var order = [];
var collection = [1, 3, 2];
var iterator = function(num, done) {
  setTimeout(function() {
    order.push(num);
    done();
  }, num * 10);
};
async.eachLimit(collection, 2, iterator, function(err) {
  assert.deepEqual(order, [1, 3, 2]);
});

### every(collection, iterator, [callback], [thisArg])

Arguments

  1. collection (Array|Object): The collection to iterate over.
  2. iterator(item, callback) (Function): The function called per iteration.
  3. callback(bool) (Function): The function called at the end.
  4. thisArg (*): The this binding of iterator.
var order = [];
var collection = [1, 3, 2, 4];
var iterator = function(num, callback) {
  setTimeout(function() {
    order.push(num);
    callback(num % 2);
  }, num * 10);
};
async.every(collection, iterator, function(bool) {
  assert.strictEqual(bool, false);
  assert.deepEqual(order, [1, 2]);
  done();
});


### everySeries(collection, iterator, [callback], [thisArg])

Arguments

  1. collection (Array|Object): The collection to iterate over.
  2. iterator(item, callback) (Function): The function called per iteration.
  3. callback(bool) (Function): The function called at the end.
  4. thisArg (*): The this binding of iterator.
var order = [];
var collection = [1, 3, 2, 4];
var iterator = function(num, callback) {
  setTimeout(function() {
    order.push(num);
    callback(num % 2);
  }, num * 10);
};
async.everySeries(collection, iterator, function(bool) {
  assert.strictEqual(bool, false);
  assert.deepEqual(order, [1, 3, 2]);
  done();
});

### everyLimit(collection, iterator, [callback], [thisArg])

Arguments

  1. collection (Array|Object): The collection to iterate over.
  2. limit (Number): The maximum number of iterators to run at any time.
  3. iterator(item, callback) (Function): The function called per iteration.
  4. callback(bool) (Function): The function called at the end.
  5. thisArg (*): The this binding of iterator.
var limit = 2;
var order = [];
var collection = [1, 3, 2, 4];
var iterator = function(num, callback) {
  setTimeout(function() {
    order.push(num);
    callback(num % 2);
  }, num * 10);
};
async.everyLimit(collection, limit, iterator, function(bool) {
  assert.strictEqual(bool, false);
  assert.deepEqual(order, [1, 3, 2]);
  done();
});

### filter(collection, iterator, [callback], [thisArg])

Arguments

  1. collection (Array|Object): The collection to iterate over.
  2. iterator(item, callback) (Function): The function called per iteration.
  3. callback(array) (Function): The function called at the end.
  4. thisArg (*): The this binding of iterator.
var order = [];
var collection = [3, 1, 2, 4];
var iterator = function(num, done) {
  setTimeout(function() {
    order.push(num);
    done(num % 2);
  }, num * 10);
};
async.filter(collection, iterator, function(array) {
  assert.deepEqual(array, [3, 1]);
  assert.deepEqual(order, [1, 2, 3, 4]);
});

### filterSeries(collection, iterator, [callback], [thisArg])

Arguments

  1. collection (Array|Object): The collection to iterate over.
  2. iterator(item, callback) (Function): The function called per iteration.
  3. callback(array) (Function): The function called at the end.
  4. thisArg (*): The this binding of iterator.
var order = [];
var collection = [3, 1, 2, 4];
var iterator = function(num, done) {
  setTimeout(function() {
    order.push(num);
    done(num % 2);
  }, num * 10);
};
async.filterSeries(collection, iterator, function(array) {
  assert.deepEqual(array, [3, 1]);
  assert.deepEqual(order, [3, 1, 2, 4]);
});

### filterLimit(collection, limit, iterator, [callback], [thisArg])

Arguments

  1. collection (Array|Object): The collection to iterate over.
  2. limit (Number): The maximum number of iterators to run at any time.
  3. iterator(item, callback) (Function): The function called per iteration.
  4. callback(array) (Function): The function called at the end.
  5. thisArg (*): The this binding of iterator.
var order = [];
var collection = [3, 1, 2, 4];
var iterator = function(num, done) {
  setTimeout(function() {
    order.push(num);
    done(num % 2);
  }, num * 10);
};
async.filterLimit(collection, 2, iterator, function(array) {
  assert.deepEqual(array, [3, 1]);
  assert.deepEqual(order, [1, 3, 2, 4]);
});

### map(collection, iterator, [callback], [thisArg])

Arguments

  1. collection (Array|Object): The collection to iterate over.
  2. iterator(item, callback) (Function): The function called per iteration.
  3. callback(err, array) (Function): The function called at the end.
  4. thisArg (*): The this binding of iterator.
var order = [];
var collection = [1, 3, 2];
var iterator = function(num, done) {
  setTimeout(function() {
    order.push(num);
    done(null, num);
  }, num * 10);
};
async.map(collection, iterator, function(err, array) {
  assert.deepEqual(array, [1, 3, 2]);
  assert.deepEqual(order, [1, 2, 3]);
});

### eachSeries(collection, iterator, [callback], [thisArg])

Arguments

  1. collection (Array|Object): The collection to iterate over.
  2. iterator(item, callback) (Function): The function called per iteration.
  3. callback(err, array) (Function): The function called at the end.
  4. thisArg (*): The this binding of iterator.
var order = [];
var collection = [1, 3, 2];
var iterator = function(num, done) {
  setTimeout(function() {
    order.push(num);
    done(null, num);
  }, num * 10);
};
async.mapSeries(collection, iterator, function(err, array) {
  assert.deepEqual(array, [1, 3, 2]);
  assert.deepEqual(order, [1, 3, 2]);
});

### mapLimit(collection, limit, iterator, [callback], [thisArg])

Arguments

  1. collection (Array|Object): The collection to iterate over.
  2. limit (Number): The maximum number of iterators to run at any time.
  3. iterator(item, callback) (Function): The function called per iteration.
  4. callback(err, array) (Function): The function called at the end.
  5. thisArg (*): The this binding of iterator.
var order = [];
var collection = [1, 3, 2];
var iterator = function(num, done) {
  setTimeout(function() {
    order.push(num);
    done(null, num);
  }, num * 10);
};
async.mapLimit(collection, 2, iterator, function(err, array) {
  assert.deepEqual(array, [1, 3, 2]);
  assert.deepEqual(order, [1, 3, 2]);
});

### multiEach(collection, [tasks], [callback]) This function provides asynchronous and straight-forward to deep nested each functions, in parallel.

Arguments

  1. collection (Array|Object): The collection to iterate over to tasks.
  2. tasks (Function[]): The function called in task order.
  3. callback(err) (Function): The function called at the end.

synchronous

vvar order = [];
var array = [1, 2, 3];
var tasks = [
  function(num, index, callback) {
    order.push(num);
    callback(null, array);
  },
  function(num, index, callback) {
    order.push(num);
    callback(null, array);
  },
  function(num, index, callback) {
    order.push(num);
    callback(null, array);
  },
  function(num, index, callback) {
    order.push(num);
    callback();
  }
];

// get same result
var _order = [];
array.forEach(function(num) {
  _order.push(num);
  array.forEach(function(num) {
    _order.push(num);
    array.forEach(function(num) {
      _order.push(num);
      array.forEach(function(num) {
        _order.push(num);
      });
    });
  });
});

async.multiEach(array, tasks, function(err) {
  assert.deepEqual(order, _order);
});

asynchronous

var order = [];
var array = [1, 2, 3];
var collection = {
  a: [array, array],
  b: {
    c: array,
    d: array
  }
};
var delay = [25, 10];
var tasks = [
  function(collection, key, callback) {
    setTimeout(function() {
      callback(null, array);
    }, delay.shift());
  },
  function(collection, key, callback) {
    callback(null, array);
  },
  function(value, key, callback) {
    setTimeout(function() {
      order.push(value);
      callback();
    }, value * 10);
  }
];

async.multiEach(collection, tasks, function(err) {
  assert.deepEqual(order, [
    1, 1, 1,
    2, 2, 2,
    1, 1, 1,
    3, 3, 3,
    2, 2, 2,
    3, 3, 3
  ]);
});

### pick(collection, iterator, [callback], [thisArg])

Arguments

  1. collection (Array|Object): The collection to iterate over.
  2. iterator(item, callback) (Function): The function called per iteration.
  3. callback(collection) (Function): The function called at the end.
  4. thisArg (*): The this binding of iterator.
var order = [];
var collection = [1, 3, 2, 4];
var iterator = function(num, done) {
  setTimeout(function() {
    order.push(num);
    done(num % 2);
  }, num * 10);
};
async.pick(collection, iterator, function(collection) {
  assert.deepEqual(collection, [1, 3]);
  assert.deepEqual(order, [1, 2, 3, 4]);
});

### pickSeries(collection, iterator, [callback], [thisArg])

Arguments

  1. collection (Array|Object): The collection to iterate over.
  2. iterator(item, callback) (Function): The function called per iteration.
  3. callback(collection) (Function): The function called at the end.
  4. thisArg (*): The this binding of iterator.
var order = [];
var collection = [1, 3, 2, 4];
var iterator = function(num, done) {
  setTimeout(function() {
    order.push(num);
    done(num % 2);
  }, num * 10);
};
async.pickSeries(collection, iterator, function(collection) {
  assert.deepEqual(collection, [1, 3]);
  assert.deepEqual(order, [1, 3, 2, 4]);
});

### pickLimit(collection, limit, iterator, [callback], [thisArg])

Arguments

  1. collection (Array|Object): The collection to iterate over.
  2. limit (Number): The maximum number of iterators to run at any time.
  3. iterator(item, callback) (Function): The function called per iteration.
  4. callback(collection) (Function): The function called at the end.
  5. thisArg (*): The this binding of iterator.
var order = [];
var collection = [1, 3, 2, 4];
var iterator = function(num, done) {
  setTimeout(function() {
    order.push(num);
    done(num % 2);
  }, num * 10);
};
async.pickLimit(collection, 2, iterator, function(collection) {
  assert.deepEqual(collection, [1, 3]);
  assert.deepEqual(order, [1, 3, 2, 4]);
});

### reduce(collection, accumulator, iterator, [callback], [thisArg])

Arguments

  1. collection (Array|Object): The collection to iterate over.
  2. accumulator (*): Initial value of the accumulator.
  3. iterator(memo, item, callback) (Function): The function called per iteration.
  4. callback(err, result) (Function): The function called at the end.
  5. thisArg (*): The this binding of iterator.
var order = [];
var collection = [1, 3, 2, 4];
var iterator = function(memo, num, done) {
  setTimeout(function() {
    order.push(num);
    done(null, memo + num);
  }, num * 10);
};
async.reduce(collection, 0, iterator, function(err, result) {
  assert.strictEqual(result, 10);
  assert.deepEqual(order, [1, 3, 2, 4]);
});

### reduceRight(collection, accumulator, iterator, [callback], [thisArg])

Arguments

  1. collection (Array|Object): The collection to iterate over.
  2. accumulator (*): Initial value of the accumulator.
  3. iterator(memo, item, callback) (Function): The function called per iteration.
  4. callback(err, result) (Function): The function called at the end.
  5. thisArg (*): The this binding of iterator.
var order = [];
var collection = [1, 3, 2, 4];
var iterator = function(memo, num, done) {
  setTimeout(function() {
    order.push(num);
    done(null, memo + num);
  }, num * 10);
};
async.reduceRight(collection, 0, iterator, function(err, result) {
  assert.strictEqual(result, 10);
  assert.deepEqual(order, [4, 2, 3, 1]);
});

### reject(collection, iterator, [callback], [thisArg])

Arguments

  1. collection (Array|Object): The collection to iterate over.
  2. iterator(item, callback) (Function): The function called per iteration.
  3. callback(collection) (Function): The function called at the end.
  4. thisArg (*): The this binding of iterator.
var order = [];
var collection = [1, 3, 2, 4];
var iterator = function(num, done) {
  setTimeout(function() {
    order.push(num);
    done(num % 2);
  }, num * 10);
};
async.reject(collection, iterator, function(collection) {
  assert.deepEqual(collection, [2, 4]);
  assert.deepEqual(order, [1, 2, 3, 4]);
});

### rejectSeries(collection, iterator, [callback], [thisArg])

Arguments

  1. collection (Array|Object): The collection to iterate over.
  2. iterator(item, callback) (Function): The function called per iteration.
  3. callback(collection) (Function): The function called at the end.
  4. thisArg (*): The this binding of iterator.
var order = [];
var collection = [1, 3, 2, 4];
var iterator = function(num, done) {
  setTimeout(function() {
    order.push(num);
    done(num % 2);
  }, num * 10);
};
async.rejectSeries(collection, iterator, function(collection) {
  assert.deepEqual(collection, [2, 4]);
  assert.deepEqual(order, [1, 3, 2, 4]);
});

### rejectLimit(collection, limit, iterator, [callback], [thisArg])

Arguments

  1. collection (Array|Object): The collection to iterate over.
  2. limit (Number): The maximum number of iterators to run at any time.
  3. iterator(item, callback) (Function): The function called per iteration.
  4. callback(collection) (Function): The function called at the end.
  5. thisArg (*): The this binding of iterator.
var order = [];
var collection = [1, 3, 2, 4];
var iterator = function(num, done) {
  setTimeout(function() {
    order.push(num);
    done(num % 2);
  }, num * 10);
};
async.rejectLimit(collection, 2, iterator, function(collection) {
  assert.deepEqual(collection, [2, 4]);
  assert.deepEqual(order, [1, 3, 2, 4]);
});

### some(collection, iterator, [callback], [thisArg])

Arguments

  1. collection (Array|Object): The collection to iterate over.
  2. iterator(item, callback) (Function): The function called per iteration.
  3. callback(bool) (Function): The function called at the end.
  4. thisArg (*): The this binding of iterator.
var order = [];
var collection = [3, 1, 2, 4];
var iterator = function(num, done) {
  setTimeout(function() {
    order.push(num);
    done(num % 2);
  }, num * 10);
};
async.some(collection, iterator, function(bool) {
  assert.ok(bool);
  assert.deepEqual(order, [1]);
});

### someSeries(collection, iterator, [callback], [thisArg])

Arguments

  1. collection (Array|Object): The collection to iterate over.
  2. iterator(item, callback) (Function): The function called per iteration.
  3. callback(bool) (Function): The function called at the end.
  4. thisArg (*): The this binding of iterator.
var order = [];
var collection = [3, 1, 2, 4];
var iterator = function(num, done) {
  setTimeout(function() {
    order.push(num);
    done(num % 2);
  }, num * 10);
};
async.someSeries(collection, iterator, function(bool) {
  assert.ok(bool);
  assert.deepEqual(order, [3]);
});

### someLimit(collection, iterator, [callback], [thisArg])

Arguments

  1. collection (Array|Object): The collection to iterate over.
  2. limit (Number): The maximum number of iterators to run at any time.
  3. iterator(item, callback) (Function): The function called per iteration.
  4. callback(bool) (Function): The function called at the end.
  5. thisArg (*): The this binding of iterator.
var order = [];
var collection = [3, 1, 2, 4];
var iterator = function(num, done) {
  setTimeout(function() {
    order.push(num);
    done(num % 2);
  }, num * 10);
};
async.someLimit(collection, 2, iterator, function(bool) {
  assert.ok(bool);
  assert.deepEqual(order, [1]);
});

### sortBy(collection, iterator, [callback], [thisArg])

Arguments

  1. collection (Array|Object): The collection to iterate over.
  2. iterator(item, callback) (Function): The function called per iteration.
  3. callback(err, array) (Function): The function called at the end.
  4. thisArg (*): The this binding of iterator.
var order = [];
var collection = [3, 1, 4, 2];
var iterator = function(num, done) {
  setTimeout(function() {
    order.push(num);
    done(null, num % 2);
  }, num * 10);
};
async.sortBy(collection, iterator, function(err, array) {
  assert.deepEqual(array, [4, 2, 3, 1]);
  assert.deepEqual(order, [1, 2, 3, 4]);
});

### sortBySeries(collection, iterator, [callback], [thisArg])

Arguments

  1. collection (Array|Object): The collection to iterate over.
  2. iterator(item, callback) (Function): The function called per iteration.
  3. callback(err, array) (Function): The function called at the end.
  4. thisArg (*): The this binding of iterator.
var order = [];
var collection = [3, 1, 4, 2];
var iterator = function(num, done) {
  setTimeout(function() {
    order.push(num);
    done(null, num % 2);
  }, num * 10);
};
async.sortBySeries(collection, iterator, function(err, array) {
  assert.deepEqual(array, [4, 2, 3, 1]);
  assert.deepEqual(order, [3, 1, 4, 2]);
});

### sortByLimit(collection, iterator, [callback], [thisArg])

Arguments

  1. collection (Array|Object): The collection to iterate over.
  2. limit (Number): The maximum number of iterators to run at any time.
  3. iterator(item, callback) (Function): The function called per iteration.
  4. callback(err, array) (Function): The function called at the end.
  5. thisArg (*): The this binding of iterator.
var order = [];
var collection = [3, 1, 4, 2];
var iterator = function(num, done) {
  setTimeout(function() {
    order.push(num);
    done(null, num % 2);
  }, num * 10);
};
async.sortByLimit(collection, 2, iterator, function(err, array) {
  assert.deepEqual(array, [4, 2, 3, 1]);
  assert.deepEqual(order, [1, 3, 2, 4]);
});

### transform(collection, iterator, [callback], [accumulator], [thisArg]) This function is similar to lodash transform, in parallel.

Arguments

  1. collection (Array|Object): The collection to iterate over.
  2. iterator(memo, item, callback) (Function): The function called per iteration.
  3. callback(err, result) (Function): The function called at the end.
  4. accumulator (*): Initial value of the accumulator.
  5. thisArg (*): The this binding of iterator.
var order = [];
var collection = [1, 5, 3, 2, 4];
var iterator = function(memo, num, index, done) {
  setTimeout(function() {
    order.push(num);
    if (num % 2) {
      memo.push(num);
    }
    done();
  }, num * 10);
};
async.transform(collection, iterator, function(err, result) {
  assert.deepEqual(result, [1, 3, 5]);
  assert.deepEqual(order, [1, 2, 3, 4, 5]);
});

### transformSeries(collection, iterator, [callback], [accumulator], [thisArg])

Arguments

  1. collection (Array|Object): The collection to iterate over.
  2. iterator(memo, item, callback) (Function): The function called per iteration.
  3. callback(err, result) (Function): The function called at the end.
  4. accumulator (*): Initial value of the accumulator.
  5. thisArg (*): The this binding of iterator.
var order = [];
var collection = [1, 5, 3, 2, 4];
var iterator = function(memo, num, index, done) {
  setTimeout(function() {
    order.push(num);
    if (num % 2) {
      memo.push(num);
    }
    done();
  }, num * 10);
};
async.transformSeries(collection, iterator, function(err, result) {
  assert.deepEqual(result, [1, 5, 3]);
  assert.deepEqual(order, [1, 5, 3, 2, 4]);
});

### transformLimit(collection, limit, iterator, [callback], [accumulator], [thisArg])

Arguments

  1. collection (Array|Object): The collection to iterate over.
  2. limit (Number): The maximum number of iterators to run at any time.
  3. iterator(item, callback) (Function): The function called per iteration.
  4. callback(err, array) (Function): The function called at the end.
  5. accumulator (*): Initial value of the accumulator.
  6. thisArg (*): The this binding of iterator.
var order = [];
var collection = [1, 5, 3, 2, 4];
var iterator = function(memo, num, index, done) {
  setTimeout(function() {
    order.push(num);
    if (num % 2) {
      memo.push(num);
    }
    done();
  }, num * 10);
};
async.transformLimit(collection, 2, iterator, function(err, result) {
  assert.deepEqual(result, [1, 5, 3]);
  assert.deepEqual(order, [1, 5, 2, 3, 4]);
});

### parallel(tasks, [callback], [thisArg])

Arguments

  1. tasks (Array|Object): The tasks is some functions, and they will be called in parallel.
  2. callback(err, collection) (Function): The function called at the end.
  3. thisArg (*): The this binding of iterator.
var order = [];
var tasks = [
  function(done) {
    setTimeout(function() {
      order.push(1);
      done(null, 1);
    }, 10);
  },
  function(done) {
    setTimeout(function() {
      order.push(3);
      done(null, 3);
    }, 30);
  },
  function(done) {
    setTimeout(function() {
      order.push(4);
      done(null, 4);
    }, 40);
  },
  function(done) {
    setTimeout(function() {
      order.push(2);
      done(null, 2);
    }, 10);
  }
];

async.parallel(tasks, function(err, res) {
  assert.deepEqual(res, [1, 3, 4, 2]);
  assert.deepEqual(order, [1, 2, 3, 4]);
});

### series(tasks, [callback], [thisArg])

Arguments

  1. tasks (Array|Object): The tasks is some functions, and they will be called in series.
  2. callback(err, collection) (Function): The function called at the end.
  3. thisArg (*): The this binding of iterator.
var order = [];
var tasks = [
  function(done) {
    setTimeout(function() {
      order.push(1);
      done(null, 1);
    }, 10);
  },
  function(done) {
    setTimeout(function() {
      order.push(3);
      done(null, 3);
    }, 30);
  },
  function(done) {
    setTimeout(function() {
      order.push(4);
      done(null, 4);
    }, 40);
  },
  function(done) {
    setTimeout(function() {
      order.push(2);
      done(null, 2);
    }, 10);
  }
];

async.series(tasks, function(err, res) {
  assert.deepEqual(res, [1, 3, 4, 2]);
  assert.deepEqual(order, [1, 3, 4, 2]);
});

### parallelLimit(tasks, limit, [callback], [thisArg])

Arguments

  1. tasks (Array|Object): The tasks is some functions, and they will be called in limited parallel.
  2. limit (Number): The maximum number of iterators to run at any time.
  3. callback(err, collection) (Function): The function called at the end.
  4. thisArg (*): The this binding of iterator.
var order = [];
var tasks = [
  function(done) {
    setTimeout(function() {
      order.push(1);
      done(null, 1);
    }, 10);
  },
  function(done) {
    setTimeout(function() {
      order.push(3);
      done(null, 3);
    }, 30);
  },
  function(done) {
    setTimeout(function() {
      order.push(4);
      done(null, 4);
    }, 40);
  },
  function(done) {
    setTimeout(function() {
      order.push(2);
      done(null, 2);
    }, 10);
  }
];

async.parallelLimit(tasks, 2, function(err, res) {
  assert.deepEqual(res, [1, 3, 4, 2]);
  assert.deepEqual(order, [1, 3, 2, 4]);
});

### waterfall(tasks, [callback], [thisArg])

Arguments

  1. tasks (Array|Object): The tasks is some functions, and they will be called in series.
  2. callback(err, res) (Function): The function called at the end.
  3. thisArg (*): The this binding of iterator.
var tasks = [
  function(done) {
    setTimeout(function() {
      done(null, 1);
    }, 10);
  },
  function(num, done) {
    assert.strictEqual(num, 1);
    setTimeout(function() {
      done(null, 3);
    }, 30);
  },
  function(num, done) {
    assert.strictEqual(num, 3);
    setTimeout(function() {
      done(null, 2, 4);
    }, 20);
  },
  function(num1, num2, done) {
    assert.strictEqual(num1, 2);
    assert.strictEqual(num2, 4);
    setTimeout(function() {
      done(null, 4);
    }, 40);
  }
];
async.waterfall(tasks, function(err, res) {
  assert.strictEqual(res, 4);
});

Speed Comparison

sample script

sample.waterfall.js
'use strict';
var comparator = require('func-comparator');
var _ = require('lodash');
var async = require('async');
var neo_async = require('neo-async');

// roop count
var count = 10;
// sampling times
var times = 1000;
var array = _.shuffle(_.times(count));
var tasks = _.map(array, function(n, i) {
  if (i === 0) {
    return function(next) {
      next(null, n);
    };
  }
  return function(total, next) {
    next(null, total + n);
  };
});
var funcs = {
  'async': function(callback) {
    async.waterfall(tasks, callback);
  },
  'neo-async': function(callback) {
    neo_async.waterfall(tasks, callback);
  }
};

comparator
.set(funcs)
.option({
  async: true,
  times: times
})
.start()
.result(function(err, res) {
  console.log(res);
});

execute

# using garbage collection per execute
$ node --exsepo_gc speed_test/controlFlow/sample.waterfall.js

result

{ async:
   { min: 91.79,
     max: 1751.06,
     average: 275.01,
     variance: 55185.67,
     standard_deviation: 234.91,
     vs: { 'neo-async': 41 } }, //[%] 100 * neo_async.average / async.average
  'neo-async':
   { min: 16.55,
     max: 600.16,
     average: 112.78,
     variance: 11310.35,
     standard_deviation: 106.35,
     vs: { async: 243.84 } } }

Results

Collections

functioncounttimesasync/neo-async (node)async/neo-async (iojs)
concat101000125.77149.21
concatSeries101000101.81125.75
detect101000282.77
detectSeries101000112.06
each101000111.66
eachSeries10100094.08
eachLimit101000154.82
every101000217.7
filter101000279.27
filterSeries101000242.97
map101000473.08
mapSeries101000359.44
mapLimit101000590.42
reduce101000102.36
reduceRight101000341.52
some101000266.78
sortBy101000135.18

ControlFlow

functioncounttimesasync/neo-async (node)async/neo-async (iojs)
waterfall101000467.34775.08
waterfall501000795.151630.62
parallel101000406.26341.25
parallelLimit101000549.05330.17
series10500316.32326.77

jsperf Comparison

Collecitons

functionurl
eachhttp://jsperf.com/async-each
eachSerieshttp://jsperf.com/async-each-series
eachLimithttp://jsperf.com/async-each-limit
maphttp://jsperf.com/async-map/2
mapSerieshttp://jsperf.com/async-map-series

ControlFlow

funcitonurl
waterfallhttp://jsperf.com/async-waterfall/3
parallelhttp://jsperf.com/async-parallel/3
parallelLimithttp://jsperf.com/async-parallel-limit
serieshttp://jsperf.com/async-series/6

Keywords

FAQs

Last updated on 29 Jan 2015

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc