- Source:
Examples
// array
var order = [];
var array = [1, 5, 3, 4, 2];
var iterator = function(result, num, done) {
setTimeout(function() {
order.push(num);
result.push(num);
done();
}, num * 10);
};
async.transformLimit(array, 2, iterator, function(err, res) {
console.log(res); // [1, 3, 5, 2, 4]
console.log(order); // [1, 3, 5, 2, 4]
});
// array with index and accumulator
var order = [];
var array = [1, 5, 3, 4, 2];
var iterator = function(result, num, index, done) {
setTimeout(function() {
order.push([num, index]);
result[index] = key;
done();
}, num * 10);
};
async.transformLimit(array, 2, {}, iterator, function(err, res) {
console.log(res); // { '0': 1, '1': 5, '2': 3, '3': 4, '4': 2 }
console.log(order); // [[1, 0], [3, 2], [5, 1], [2, 4], [4, 3]]
});
// object with accumulator
var order = [];
var object = { a: 1, b: 5, c: 3, d: 4, e: 2 };
var iterator = function(result, num, done) {
setTimeout(function() {
order.push(num);
result.push(num);
done();
}, num * 10);
};
async.transformLimit(object, 2, [], iterator, function(err, res) {
console.log(res); // [1, 3, 5, 2, 4]
console.log(order); // [1, 3, 5, 2, 4]
});
// object with key
var order = [];
var object = { a: 1, b: 5, c: 3, d: 4, e: 2 };
var iterator = function(result, num, key, done) {
setTimeout(function() {
order.push([num, key]);
result[key] = num;
done();
}, num * 10);
};
async.transformLimit(object, 2, iterator, function(err, res) {
console.log(res); // { a: 1, b: 5, c: 3, d: 4, e: 2 };
console.log(order); // [[1, 'a'], [3, 'c'], [5, 'b'], [2, 'e'], [4, 'd']]
});