Neo-Async is compatible with Async.js, it is faster and has more feature.
Async is a utilty 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, res) (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, res) {
assert.deepEqual(res, [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, res) (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, res) {
assert.deepEqual(res, [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, res) (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, res) {
assert.deepEqual(res, [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(res) (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(res) {
assert.deepEqual(res, 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(res) (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(res) {
assert.deepEqual(res, 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(res) (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(res) {
assert.deepEqual(res, 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]);
});
### 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
]);
});
Speed Comparison