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