3 A module for limiting concurrent asynchronous actions in flight. Forked from [queue](https://github.com/jessetane/queue).
5 [![npm](http://img.shields.io/npm/v/async-limiter.svg?style=flat-square)](http://www.npmjs.org/async-limiter)
6 [![tests](https://img.shields.io/travis/STRML/async-limiter.svg?style=flat-square&branch=master)](https://travis-ci.org/STRML/async-limiter)
7 [![coverage](https://img.shields.io/coveralls/STRML/async-limiter.svg?style=flat-square&branch=master)](https://coveralls.io/r/STRML/async-limiter)
9 This module exports a class `Limiter` that implements some of the `Array` API.
10 Pass async functions (ones that accept a callback or return a promise) to an instance's additive array methods.
14 Certain functions, like `zlib`, have [undesirable behavior](https://github.com/nodejs/node/issues/8871#issuecomment-250915913) when
15 run at infinite concurrency.
17 In this case, it is actually faster, and takes far less memory, to limit concurrency.
19 This module should do the absolute minimum work necessary to queue up functions. PRs are welcome that would
20 make this module faster or lighter, but new functionality is not desired.
22 Style should confirm to nodejs/node style.
27 var Limiter = require('async-limiter')
29 var t = new Limiter({concurrency: 2});
32 // add jobs using the familiar Array API
33 t.push(function (cb) {
49 t.unshift(function (cb) {
54 t.splice(2, 0, function (cb) {
59 // Jobs run automatically. If you want a callback when all are done,
61 t.onDone(function () {
62 console.log('all done:', results)
69 const zlib = require('zlib');
70 const Limiter = require('async-limiter');
72 const message = {some: "data"};
73 const payload = new Buffer(JSON.stringify(message));
75 // Try with different concurrency values to see how this actually
76 // slows significantly with higher concurrency!
80 // Infinity: 4423.300ms
82 const t = new Limiter({concurrency: 5});
83 function deflate(payload, cb) {
84 t.push(function(done) {
85 zlib.deflate(payload, function(err, buffer) {
92 console.time('deflate');
93 for(let i = 0; i < 30000; ++i) {
94 deflate(payload, function (err, buffer) {});
97 console.timeEnd('deflate');
103 `npm install async-limiter`
111 ### `var t = new Limiter([opts])`
112 Constructor. `opts` may contain inital values for:
118 `fn` will be called once and only once, when the queue is empty.
120 ## Instance methods mixed in from `Array`
121 Mozilla has docs on how these methods work [here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array).
122 ### `t.push(element1, ..., elementN)`
123 ### `t.unshift(element1, ..., elementN)`
124 ### `t.splice(index , howMany[, element1[, ...[, elementN]]])`
128 Max number of jobs the queue should process concurrently, defaults to `Infinity`.
131 Jobs pending + jobs to process (readonly).