Basically a forEachAsync that allows n async calls at once.
Another way to think of it is as a thread pool for JavaScript.
Say you have 500 http requests that you want to get done
10 at a time in batches of 400, 50, and 50 and you want
to know when each batch (and all batches) have finished... lateral is your guy!
Node.JS (Server):
npm install lateralYou can install from bower:
bower install lateralOr download the raw file from https://raw.github.com/FuturesJS/lateral/master/lateral.js:
wget https://raw.github.com/FuturesJS/lateral/master/lateral.jsOr build with pakmanager:
pakmanager build lateral;(function () {
'use strict';
var Lateral = window.Lateral || require('lateral').Lateral
, maxCallsAtOnce = 4 // default
, lateral
;
function onEach(complete, item, i) {
setTimeout(function () {
console.log(item);
complete();
}, 500);
}
lateral = Lateral.create(onEach, maxCallsAtOnce);
lateral.add(['a', 'b', 'c', 'd']).then(function () {
console.log('first batch done');
});
lateral.add(['d', 'e', 'f', 'g']).then(function () {
console.log('second batch done');
});
lateral.then(function () {
console.log('did all the things');
});
}());lateral = Lateral.create(fn, n)- create a Lateral that will execute
fnon each item to do at mostnthings at once
- create a Lateral that will execute
lateral.add(arr).then(cb)- addsarrto be handled byfnandcbis called when all inarrare handledlateral.then(callback)- Fires
callbackwhen all items in added arrays have been handled
- Fires
The code is a little hairy and could use some cleaning.