Async is a utility module which provides staright-forward.
### concat(collection, iterator, [callback], [thisArg])
Arguments
- collection (Array|Object): The collection to iterate over.
- iterator(item, callback) (Function): The function called per iteration.
- callback(err, array) (Function): The function called at the end.
- 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
- collection (Array|Object): The collection to iterate over.
- iterator(item, callback) (Function): The function called per iteration.
- callback(err, array) (Function): The function called at the end.
- 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
- collection (Array|Object): The collection to iterate over.
- limit (Number): The maximum number of iterators to run at any time.
- iterator(item, callback) (Function): The function called per iteration.
- callback(err, array) (Function): The function called at the end.
- 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
- collection (Array|Object): The collection to iterate over.
- iterator(item, callback) (Function): The function called per iteration.
- callback(item) (Function): The function called at the end.
- 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
- collection (Array|Object): The collection to iterate over.
- iterator(item, callback) (Function): The function called per iteration.
- callback(item) (Function): The function called at the end.
- 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
- collection (Array|Object): The collection to iterate over.
- limit (Number): The maximum number of iterators to run at any time.
- iterator(item, callback) (Function): The function called per iteration.
- callback(item) (Function): The function called at the end.
- 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]);
});
### eachSeries(collection, iterator, [callback], [thisArg])
The same as each, in series.
Aliases
async.forEachSeries
Arguments
- collection (Array|Object): The collection to iterate over.
- iterator(item, callback) (Function): The function called per iteration.
- callback(err) (Function): The function called at the end.
- 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
- collection (Array|Object): The collection to iterate over.
- limit (Number): The maximum number of iterators to run at any time.
- iterator(item, callback) (Function): The function called per iteration.
- callback(err) (Function): The function called at the end.
- 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
- collection (Array|Object): The collection to iterate over.
- iterator(item, callback) (Function): The function called per iteration.
- callback(bool) (Function): The function called at the end.
- 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
- collection (Array|Object): The collection to iterate over.
- iterator(item, callback) (Function): The function called per iteration.
- callback(bool) (Function): The function called at the end.
- 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
- collection (Array|Object): The collection to iterate over.
- limit (Number): The maximum number of iterators to run at any time.
- iterator(item, callback) (Function): The function called per iteration.
- callback(bool) (Function): The function called at the end.
- 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
- collection (Array|Object): The collection to iterate over.
- iterator(item, callback) (Function): The function called per iteration.
- callback(array) (Function): The function called at the end.
- 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
- collection (Array|Object): The collection to iterate over.
- iterator(item, callback) (Function): The function called per iteration.
- callback(array) (Function): The function called at the end.
- 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
- collection (Array|Object): The collection to iterate over.
- limit (Number): The maximum number of iterators to run at any time.
- iterator(item, callback) (Function): The function called per iteration.
- callback(array) (Function): The function called at the end.
- 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
- collection (Array|Object): The collection to iterate over.
- iterator(item, callback) (Function): The function called per iteration.
- callback(err, array) (Function): The function called at the end.
- 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
- collection (Array|Object): The collection to iterate over.
- iterator(item, callback) (Function): The function called per iteration.
- callback(err, array) (Function): The function called at the end.
- 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
- collection (Array|Object): The collection to iterate over.
- limit (Number): The maximum number of iterators to run at any time.
- iterator(item, callback) (Function): The function called per iteration.
- callback(err, array) (Function): The function called at the end.
- 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
- collection (Array|Object): The collection to iterate over to tasks.
- tasks (Function[]): The function called in task order.
- 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();
}
];
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
- collection (Array|Object): The collection to iterate over.
- iterator(item, callback) (Function): The function called per iteration.
- callback(collection) (Function): The function called at the end.
- 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
- collection (Array|Object): The collection to iterate over.
- iterator(item, callback) (Function): The function called per iteration.
- callback(collection) (Function): The function called at the end.
- 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
- collection (Array|Object): The collection to iterate over.
- limit (Number): The maximum number of iterators to run at any time.
- iterator(item, callback) (Function): The function called per iteration.
- callback(collection) (Function): The function called at the end.
- 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
- collection (Array|Object): The collection to iterate over.
- accumulator (*): Initial value of the accumulator.
- iterator(memo, item, callback) (Function): The function called per iteration.
- callback(err, result) (Function): The function called at the end.
- 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
- collection (Array|Object): The collection to iterate over.
- accumulator (*): Initial value of the accumulator.
- iterator(memo, item, callback) (Function): The function called per iteration.
- callback(err, result) (Function): The function called at the end.
- 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
- collection (Array|Object): The collection to iterate over.
- iterator(item, callback) (Function): The function called per iteration.
- callback(collection) (Function): The function called at the end.
- 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
- collection (Array|Object): The collection to iterate over.
- iterator(item, callback) (Function): The function called per iteration.
- callback(collection) (Function): The function called at the end.
- 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
- collection (Array|Object): The collection to iterate over.
- limit (Number): The maximum number of iterators to run at any time.
- iterator(item, callback) (Function): The function called per iteration.
- callback(collection) (Function): The function called at the end.
- 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]);
});
### someLimit(collection, iterator, [callback], [thisArg])
Arguments
- collection (Array|Object): The collection to iterate over.
- limit (Number): The maximum number of iterators to run at any time.
- iterator(item, callback) (Function): The function called per iteration.
- callback(bool) (Function): The function called at the end.
- 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
- collection (Array|Object): The collection to iterate over.
- iterator(item, callback) (Function): The function called per iteration.
- callback(err, array) (Function): The function called at the end.
- 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
- collection (Array|Object): The collection to iterate over.
- iterator(item, callback) (Function): The function called per iteration.
- callback(err, array) (Function): The function called at the end.
- 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
- collection (Array|Object): The collection to iterate over.
- limit (Number): The maximum number of iterators to run at any time.
- iterator(item, callback) (Function): The function called per iteration.
- callback(err, array) (Function): The function called at the end.
- 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
- collection (Array|Object): The collection to iterate over.
- iterator(memo, item, callback) (Function): The function called per iteration.
- callback(err, result) (Function): The function called at the end.
- accumulator (*): Initial value of the accumulator.
- 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
- collection (Array|Object): The collection to iterate over.
- iterator(memo, item, callback) (Function): The function called per iteration.
- callback(err, result) (Function): The function called at the end.
- accumulator (*): Initial value of the accumulator.
- 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
- collection (Array|Object): The collection to iterate over.
- limit (Number): The maximum number of iterators to run at any time.
- iterator(item, callback) (Function): The function called per iteration.
- callback(err, array) (Function): The function called at the end.
- accumulator (*): Initial value of the accumulator.
- 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]);
});
Speed Comparison