Namespace: groupByLimit

async.groupByLimit

Source:

Examples

// array
var order = [];
var array = [1.1, 5.9, 3.2, 3.9, 2.1];
var iterator = function(num, done) {
  setTimeout(function() {
    order.push(num);
    done(null, Math.floor(num));
  }, num * 10);
};
async.groupByLimit(array, 2, iterator, function(err, res) {
  console.log(res); // { '1': [1.1], '3': [3.2, 3.9], '5': [5.9], '2': [2.1] }
  console.log(order); // [1.1, 3.2, 5.9, 2.1, 3.9]
});
// array with index
var order = [];
var array = [1.1, 5.9, 3.2, 3.9, 2.1];
var iterator = function(num, index, done) {
  setTimeout(function() {
    order.push([num, index]);
    done(null, Math.floor(num));
  }, num * 10);
};
async.groupByLimit(array, 2, iterator, function(err, res) {
  console.log(res); // { '1': [1.1], '3': [3.2, 3.9], '5': [5.9], '2': [2.1] }
  console.log(order); // [[1.1, 0], [3.2, 2], [5.9, 1], [2.1, 4], [3.9, 3]]
});
// object
var order = [];
var object = { a: 1.1, b: 5.9, c: 3.2, d: 3.9, e: 2.1 }
var iterator = function(num, done) {
  setTimeout(function() {
    order.push(num);
    done(null, Math.floor(num));
  }, num * 10);
};
async.groupByLimit(object, 2, iterator, function(err, res) {
  console.log(res); // { '1': [1.1], '3': [3.2, 3.9], '5': [5.9], '2': [2.1] }
  console.log(order); // [1.1, 3.2, 5.9, 2.1, 3.9]
});
// object with key
var order = [];
var object = { a: 1.1, b: 5.9, c: 3.2, d: 3.9, e: 2.1 }
var iterator = function(num, key, done) {
  setTimeout(function() {
    order.push([num, key]);
    done(null, Math.floor(num));
  }, num * 10);
};
async.groupByLimit(object, 2, iterator, function(err, res) {
  console.log(res); // { '1': [1.1], '3': [3.2, 3.9], '5': [5.9], '2': [2.1] }
  console.log(order); // [[1.1, 'a'], [3.2, 'c'], [5.9, 'b'], [2.1, 'e'], [3.9, 'd']]
});