How can I create a maximum calling limit in a function?
the target function must be asynchronous
we use U.queue(name, maximum_calls, fn(next)) method (documentation)
function someFunction(number, callback) {
// U.queue(name, maximum, fn);
U.queue('someFunction', 10, function(next) {
// This context will be executed max. 10x at a time.
console.log('PROCESSED:', number);
setTimeout(function() {
// IMPORTANT: it processes next item
next();
callback();
}, 1000);
});
}
Testing:
for (var i = 0; i < 100; i++)
someFunction(i, NOOP);
How can I create a maximum calling limit in a function?