{"_id":"p-queue-cjs","_rev":"4-098ab7a3ebd55cc03c05761b2d73c4ed","time":{"created":"2023-06-09T12:57:40.851Z","7.2.0":"2022-05-19T12:34:49.791Z","modified":"2023-06-09T13:25:07.118Z","7.2.1":"2022-05-22T11:00:10.104Z","7.3.3":"2023-06-09T12:57:41.030Z","7.3.4":"2023-06-09T13:25:07.007Z"},"name":"p-queue-cjs","dist-tags":{"latest":"7.3.4"},"versions":{"7.3.3":{"name":"p-queue-cjs","version":"7.3.3","description":"Promise queue with concurrency control. A fork of p-queue for commonjs.","repository":{"type":"git","url":"git+https://github.com/nguyenngoclongdev/p-queue-cjs.git"},"bugs":{"url":"https://github.com/nguyenngoclongdev/p-queue-cjs/issues"},"sponsor":{"url":"https://ko-fi.com/nguyenngoclong"},"funding":{"type":"individual","url":"https://ko-fi.com/nguyenngoclong"},"homepage":"https://nguyenngoclongdev.github.io","main":"dist/index.js","types":"dist/index.d.ts","engines":{"node":">=12"},"keywords":["promise","queue","enqueue","limit","limited","concurrency","throttle","throat","rate","batch","ratelimit","priority","priorityqueue","fifo","job","task","async","await","promises","bluebird"],"dependencies":{"eventemitter3":"^4.0.7","p-timeout":"4.1.0"},"devDependencies":{"@sindresorhus/tsconfig":"^2.0.0","@types/benchmark":"^2.1.1","@types/node":"^17.0.13","ava":"^5.1.1","benchmark":"^2.1.4","del-cli":"^5.0.0","delay":"^5.0.0","in-range":"2.0.0","p-defer":"3.0.0","random-int":"2.0.1","time-span":"4.0.0","ts-node":"^10.9.1","tsd":"^0.25.0","typescript":"^4.8.4","xo":"^0.44.0"},"ava":{"files":["test/**"],"extensions":{"ts":"commonjs"},"nodeArguments":["--loader=ts-node/esm"]},"xo":{"rules":{"@typescript-eslint/member-ordering":"off","@typescript-eslint/no-floating-promises":"off","@typescript-eslint/no-invalid-void-type":"off","@typescript-eslint/no-unnecessary-type-assertion":"warn"}},"author":{"name":"Nguyen Ngoc Long","url":"https://nguyenngoclongdev.github.io"},"publisher":"nguyenngoclong","license":"MIT","scripts":{"build":"del-cli dist && tsc","test":"xo && ava && del-cli dist && tsc && tsd","bench":"node --loader=ts-node/esm bench.ts"},"_id":"p-queue-cjs@7.3.3","_integrity":"sha512-5J4fliZvn/YAY6jJsPAdMiTX3IdXLihXatUQa7Bl4kWSYSrIuP7XBbAvDae8b84oGWQIm6L0NUOw8glhIYFK7A==","_resolved":"/tmp/d481725674c99e3a4a41e7f92bdbfc62/p-queue-cjs-7.3.3.tgz","_from":"file:p-queue-cjs-7.3.3.tgz","_nodeVersion":"16.20.0","_npmVersion":"8.19.4","dist":{"integrity":"sha512-5J4fliZvn/YAY6jJsPAdMiTX3IdXLihXatUQa7Bl4kWSYSrIuP7XBbAvDae8b84oGWQIm6L0NUOw8glhIYFK7A==","shasum":"e719a4348170d99458711d1d2686a6a9363bc8ce","tarball":"https://registry.npmjs.org/p-queue-cjs/-/p-queue-cjs-7.3.3.tgz","fileCount":13,"unpackedSize":47985,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQDEve4D2Sv5bzisCORVHM0xx5ol4Gwymmnk4/K8Fnwj4gIgBqQkuRFp5SWdAoI/imgvR+PMYKhPKzk57ScEduO0/Lo="}]},"_npmUser":{"name":"nguyenngoclong","email":"nguyenngoclong@live.com"},"directories":{},"maintainers":[{"name":"nguyenngoclong","email":"nguyenngoclong@live.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/p-queue-cjs_7.3.3_1686315460852_0.8705244693360439"},"_hasShrinkwrap":false},"7.3.4":{"name":"p-queue-cjs","version":"7.3.4","description":"Promise queue with concurrency control. A fork of p-queue for commonjs.","repository":{"type":"git","url":"git+https://github.com/nguyenngoclongdev/p-queue-cjs.git"},"bugs":{"url":"https://github.com/nguyenngoclongdev/p-queue-cjs/issues"},"sponsor":{"url":"https://ko-fi.com/nguyenngoclong"},"funding":{"type":"individual","url":"https://ko-fi.com/nguyenngoclong"},"homepage":"https://nguyenngoclongdev.github.io","main":"dist/index.js","types":"dist/index.d.ts","engines":{"node":">=12"},"keywords":["promise","queue","enqueue","limit","limited","concurrency","throttle","throat","rate","batch","ratelimit","priority","priorityqueue","fifo","job","task","async","await","promises","bluebird"],"dependencies":{"eventemitter3":"^4.0.7","p-timeout-cjs":"^5.0.5"},"devDependencies":{"@sindresorhus/tsconfig":"^2.0.0","@types/benchmark":"^2.1.1","@types/node":"^17.0.13","ava":"^5.1.1","benchmark":"^2.1.4","del-cli":"^5.0.0","delay":"^5.0.0","in-range":"2.0.0","p-defer":"3.0.0","random-int":"2.0.1","time-span":"4.0.0","ts-node":"^10.9.1","tsd":"^0.25.0","typescript":"^4.8.4","xo":"^0.44.0"},"ava":{"files":["test/**"],"extensions":{"ts":"commonjs"},"nodeArguments":["--loader=ts-node/esm"]},"xo":{"rules":{"@typescript-eslint/member-ordering":"off","@typescript-eslint/no-floating-promises":"off","@typescript-eslint/no-invalid-void-type":"off","@typescript-eslint/no-unnecessary-type-assertion":"warn"}},"author":{"name":"Nguyen Ngoc Long","url":"https://nguyenngoclongdev.github.io"},"publisher":"nguyenngoclong","license":"MIT","scripts":{"build":"del-cli dist && tsc","test":"xo && ava && del-cli dist && tsc && tsd","bench":"node --loader=ts-node/esm bench.ts"},"_id":"p-queue-cjs@7.3.4","_integrity":"sha512-vP0BvEAgmUEShxWBCETvxiUDnwSiCLfBRqmhdKlNvcXF/7x2yemtYLcxT1pfYELZLlyGL3grvGnq+KF5OwNZfA==","_resolved":"/tmp/6fdcfdf4728244f234d216dd50eba783/p-queue-cjs-7.3.4.tgz","_from":"file:p-queue-cjs-7.3.4.tgz","_nodeVersion":"16.20.0","_npmVersion":"8.19.4","dist":{"integrity":"sha512-vP0BvEAgmUEShxWBCETvxiUDnwSiCLfBRqmhdKlNvcXF/7x2yemtYLcxT1pfYELZLlyGL3grvGnq+KF5OwNZfA==","shasum":"22b64de6dff9e0eff63cd8a1e4cd2aa6fe2e7009","tarball":"https://registry.npmjs.org/p-queue-cjs/-/p-queue-cjs-7.3.4.tgz","fileCount":13,"unpackedSize":48006,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQDUZH4XZ9zxlN3YP8ZfyJzrpzWGn+2CJsKGLlrF35xscAIhAOIsolPikd0R7pUSa8Ksbm90FC/sHCZzqs2Klxh3rlbi"}]},"_npmUser":{"name":"nguyenngoclong","email":"nguyenngoclong@live.com"},"directories":{},"maintainers":[{"name":"nguyenngoclong","email":"nguyenngoclong@live.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/p-queue-cjs_7.3.4_1686317106802_0.7516360454801709"},"_hasShrinkwrap":false}},"maintainers":[{"name":"nguyenngoclong","email":"nguyenngoclong@live.com"}],"description":"Promise queue with concurrency control. A fork of p-queue for commonjs.","homepage":"https://nguyenngoclongdev.github.io","keywords":["promise","queue","enqueue","limit","limited","concurrency","throttle","throat","rate","batch","ratelimit","priority","priorityqueue","fifo","job","task","async","await","promises","bluebird"],"repository":{"type":"git","url":"git+https://github.com/nguyenngoclongdev/p-queue-cjs.git"},"author":{"name":"Nguyen Ngoc Long","url":"https://nguyenngoclongdev.github.io"},"bugs":{"url":"https://github.com/nguyenngoclongdev/p-queue-cjs/issues"},"license":"MIT","readme":"# p-queue-cjs\n\n> Promise queue with concurrency control. A fork of [p-queue](https://github.com/sindresorhus/p-queue) for commonjs.\n\nUseful for rate-limiting async (or sync) operations. For example, when interacting with a REST API or when doing CPU/memory intensive tasks.\n\nFor servers, you probably want a Redis-backed [job queue](https://github.com/sindresorhus/awesome-nodejs#job-queues) instead.\n\nNote that the project is feature complete. We are happy to review pull requests, but we don't plan any further development. We are also not answering email support questions.\n\n## Install\n\n```sh\nnpm install p-queue-cjs\n```\n\n**Warning:** This package is native [ESM](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules) and no longer provides a CommonJS export. If your project uses CommonJS, you'll have to [convert to ESM](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c) or use the [dynamic `import()`](https://v8.dev/features/dynamic-import) function. Please don't open issues for questions regarding CommonJS / ESM. You can also use [version 6](https://github.com/sindresorhus/p-queue/tree/v6.6.2) instead which is pretty stable. We will backport security fixes to v6 for the foreseeable future.\n\n## Usage\n\nHere we run only one promise at the time. For example, set `concurrency` to 4 to run four promises at the same time.\n\n```js\nimport PQueue from 'p-queue-cjs';\nimport got from 'got';\n\nconst queue = new PQueue({concurrency: 1});\n\n(async () => {\n\tawait queue.add(() => got('https://sindresorhus.com'));\n\tconsole.log('Done: sindresorhus.com');\n})();\n\n(async () => {\n\tawait queue.add(() => got('https://avajs.dev'));\n\tconsole.log('Done: avajs.dev');\n})();\n\n(async () => {\n\tconst task = await getUnicornTask();\n\tawait queue.add(task);\n\tconsole.log('Done: Unicorn task');\n})();\n```\n\n## API\n\n### PQueue(options?)\n\nReturns a new `queue` instance, which is an [`EventEmitter3`](https://github.com/primus/eventemitter3) subclass.\n\n#### options\n\nType: `object`\n\n##### concurrency\n\nType: `number`\\\nDefault: `Infinity`\\\nMinimum: `1`\n\nConcurrency limit.\n\n##### timeout\n\nType: `number`\n\nPer-operation timeout in milliseconds. Operations fulfill once `timeout` elapses if they haven't already.\n\n##### throwOnTimeout\n\nType: `boolean`\\\nDefault: `false`\n\nWhether or not a timeout is considered an exception.\n\n##### autoStart\n\nType: `boolean`\\\nDefault: `true`\n\nWhether queue tasks within concurrency limit, are auto-executed as soon as they're added.\n\n##### queueClass\n\nType: `Function`\n\nClass with a `enqueue` and `dequeue` method, and a `size` getter. See the [Custom QueueClass](#custom-queueclass) section.\n\n##### intervalCap\n\nType: `number`\\\nDefault: `Infinity`\\\nMinimum: `1`\n\nThe max number of runs in the given interval of time.\n\n##### interval\n\nType: `number`\\\nDefault: `0`\\\nMinimum: `0`\n\nThe length of time in milliseconds before the interval count resets. Must be finite.\n\n##### carryoverConcurrencyCount\n\nType: `boolean`\\\nDefault: `false`\n\nIf `true`, specifies that any [pending](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) Promises, should be carried over into the next interval and counted against the `intervalCap`. If `false`, any of those pending Promises will not count towards the next `intervalCap`.\n\n### queue\n\n`PQueue` instance.\n\n#### .add(fn, options?)\n\nAdds a sync or async task to the queue. Always returns a promise.\n\nNote: If your items can potentially throw an exception, you must handle those errors from the returned Promise or they may be reported as an unhandled Promise rejection and potentially cause your process to exit immediately.\n\n##### fn\n\nType: `Function`\n\nPromise-returning/async function. When executed, it will receive `{signal}` as the first argument.\n\n#### options\n\nType: `object`\n\n##### priority\n\nType: `number`\\\nDefault: `0`\n\nPriority of operation. Operations with greater priority will be scheduled first.\n\n##### signal\n\n*Requires Node.js 16 or later.*\n\n[`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) for cancellation of the operation. When aborted, it will be removed from the queue and the `queue.add()` call will reject with an `AbortError`. If the operation is already running, the signal will need to be handled by the operation itself.\n\n```js\nimport PQueue, {AbortError} from 'p-queue-cjs';\nimport got, {CancelError} from 'got';\n\nconst queue = new PQueue();\n\nconst controller = new AbortController();\n\ntry {\n\tawait queue.add(({signal}) => {\n\t\tconst request = got('https://sindresorhus.com');\n\n\t\tsignal.addEventListener('abort', () => {\n\t\t\trequest.cancel();\n\t\t});\n\n\t\ttry {\n\t\t\treturn await request;\n\t\t} catch (error) {\n\t\t\tif (!(error instanceof CancelError)) {\n\t\t\t\tthrow error;\n\t\t\t}\n\t\t}\n\t}, {signal: controller.signal});\n} catch (error) {\n\tif (!(error instanceof AbortError)) {\n\t\tthrow error;\n\t}\n}\n```\n\n#### .addAll(fns, options?)\n\nSame as `.add()`, but accepts an array of sync or async functions and returns a promise that resolves when all functions are resolved.\n\n#### .pause()\n\nPut queue execution on hold.\n\n#### .start()\n\nStart (or resume) executing enqueued tasks within concurrency limit. No need to call this if queue is not paused (via `options.autoStart = false` or by `.pause()` method.)\n\nReturns `this` (the instance).\n\n#### .onEmpty()\n\nReturns a promise that settles when the queue becomes empty.\n\nCan be called multiple times. Useful if you for example add additional items at a later time.\n\n#### .onIdle()\n\nReturns a promise that settles when the queue becomes empty, and all promises have completed; `queue.size === 0 && queue.pending === 0`.\n\nThe difference with `.onEmpty` is that `.onIdle` guarantees that all work from the queue has finished. `.onEmpty` merely signals that the queue is empty, but it could mean that some promises haven't completed yet.\n\n#### .onSizeLessThan(limit)\n\nReturns a promise that settles when the queue size is less than the given limit: `queue.size < limit`.\n\nIf you want to avoid having the queue grow beyond a certain size you can `await queue.onSizeLessThan()` before adding a new item.\n\nNote that this only limits the number of items waiting to start. There could still be up to `concurrency` jobs already running that this call does not include in its calculation.\n\n#### .clear()\n\nClear the queue.\n\n#### .size\n\nSize of the queue, the number of queued items waiting to run.\n\n#### .sizeBy(options)\n\nSize of the queue, filtered by the given options.\n\nFor example, this can be used to find the number of items remaining in the queue with a specific priority level.\n\n```js\nimport PQueue from 'p-queue-cjs';\n\nconst queue = new PQueue();\n\nqueue.add(async () => '🦄', {priority: 1});\nqueue.add(async () => '🦄', {priority: 0});\nqueue.add(async () => '🦄', {priority: 1});\n\nconsole.log(queue.sizeBy({priority: 1}));\n//=> 2\n\nconsole.log(queue.sizeBy({priority: 0}));\n//=> 1\n```\n\n#### .pending\n\nNumber of running items (no longer in the queue).\n\n#### [.timeout](#timeout)\n\n#### [.concurrency](#concurrency)\n\n#### .isPaused\n\nWhether the queue is currently paused.\n\n## Events\n\n#### active\n\nEmitted as each item is processed in the queue for the purpose of tracking progress.\n\n```js\nimport delay from 'delay';\nimport PQueue from 'p-queue-cjs';\n\nconst queue = new PQueue({concurrency: 2});\n\nlet count = 0;\nqueue.on('active', () => {\n\tconsole.log(`Working on item #${++count}.  Size: ${queue.size}  Pending: ${queue.pending}`);\n});\n\nqueue.add(() => Promise.resolve());\nqueue.add(() => delay(2000));\nqueue.add(() => Promise.resolve());\nqueue.add(() => Promise.resolve());\nqueue.add(() => delay(500));\n```\n\n#### completed\n\nEmitted when an item completes without error.\n\n```js\nimport delay from 'delay';\nimport PQueue from 'p-queue-cjs';\n\nconst queue = new PQueue({concurrency: 2});\n\nqueue.on('completed', result => {\n\tconsole.log(result);\n});\n\nqueue.add(() => Promise.resolve('hello, world!'));\n```\n\n#### error\n\nEmitted if an item throws an error.\n\n```js\nimport delay from 'delay';\nimport PQueue from 'p-queue-cjs';\n\nconst queue = new PQueue({concurrency: 2});\n\nqueue.on('error', error => {\n\tconsole.error(error);\n});\n\nqueue.add(() => Promise.reject(new Error('error')));\n```\n\n#### empty\n\nEmitted every time the queue becomes empty.\n\nUseful if you for example add additional items at a later time.\n\n#### idle\n\nEmitted every time the queue becomes empty and all promises have completed; `queue.size === 0 && queue.pending === 0`.\n\nThe difference with `empty` is that `idle` guarantees that all work from the queue has finished. `empty` merely signals that the queue is empty, but it could mean that some promises haven't completed yet.\n\n```js\nimport delay from 'delay';\nimport PQueue from 'p-queue-cjs';\n\nconst queue = new PQueue();\n\nqueue.on('idle', () => {\n\tconsole.log(`Queue is idle.  Size: ${queue.size}  Pending: ${queue.pending}`);\n});\n\nconst job1 = queue.add(() => delay(2000));\nconst job2 = queue.add(() => delay(500));\n\nawait job1;\nawait job2;\n// => 'Queue is idle.  Size: 0  Pending: 0'\n\nawait queue.add(() => delay(600));\n// => 'Queue is idle.  Size: 0  Pending: 0'\n```\n\nThe `idle` event is emitted every time the queue reaches an idle state. On the other hand, the promise the `onIdle()` function returns resolves once the queue becomes idle instead of every time the queue is idle.\n\n#### add\n\nEmitted every time the add method is called and the number of pending or queued tasks is increased.\n\n#### next\n\nEmitted every time a task is completed and the number of pending or queued tasks is decreased. This is emitted regardless of whether the task completed normally or with an error.\n\n```js\nimport delay from 'delay';\nimport PQueue from 'p-queue-cjs';\n\nconst queue = new PQueue();\n\nqueue.on('add', () => {\n\tconsole.log(`Task is added.  Size: ${queue.size}  Pending: ${queue.pending}`);\n});\n\nqueue.on('next', () => {\n\tconsole.log(`Task is completed.  Size: ${queue.size}  Pending: ${queue.pending}`);\n});\n\nconst job1 = queue.add(() => delay(2000));\nconst job2 = queue.add(() => delay(500));\n\nawait job1;\nawait job2;\n//=> 'Task is added.  Size: 0  Pending: 1'\n//=> 'Task is added.  Size: 0  Pending: 2'\n\nawait queue.add(() => delay(600));\n//=> 'Task is completed.  Size: 0  Pending: 1'\n//=> 'Task is completed.  Size: 0  Pending: 0'\n```\n\n### AbortError\n\nThe error thrown by `queue.add()` when a job is aborted before it is run. See [`signal`](#signal).\n\n## Advanced example\n\nA more advanced example to help you understand the flow.\n\n```js\nimport delay from 'delay';\nimport PQueue from 'p-queue-cjs';\n\nconst queue = new PQueue({concurrency: 1});\n\n(async () => {\n\tawait delay(200);\n\n\tconsole.log(`8. Pending promises: ${queue.pending}`);\n\t//=> '8. Pending promises: 0'\n\n\t(async () => {\n\t\tawait queue.add(async () => '🐙');\n\t\tconsole.log('11. Resolved')\n\t})();\n\n\tconsole.log('9. Added 🐙');\n\n\tconsole.log(`10. Pending promises: ${queue.pending}`);\n\t//=> '10. Pending promises: 1'\n\n\tawait queue.onIdle();\n\tconsole.log('12. All work is done');\n})();\n\n(async () => {\n\tawait queue.add(async () => '🦄');\n\tconsole.log('5. Resolved')\n})();\nconsole.log('1. Added 🦄');\n\n(async () => {\n\tawait queue.add(async () => '🐴');\n\tconsole.log('6. Resolved')\n})();\nconsole.log('2. Added 🐴');\n\n(async () => {\n\tawait queue.onEmpty();\n\tconsole.log('7. Queue is empty');\n})();\n\nconsole.log(`3. Queue size: ${queue.size}`);\n//=> '3. Queue size: 1`\n\nconsole.log(`4. Pending promises: ${queue.pending}`);\n//=> '4. Pending promises: 1'\n```\n\n```\n$ node example.js\n1. Added 🦄\n2. Added 🐴\n3. Queue size: 1\n4. Pending promises: 1\n5. Resolved 🦄\n6. Resolved 🐴\n7. Queue is empty\n8. Pending promises: 0\n9. Added 🐙\n10. Pending promises: 1\n11. Resolved 🐙\n12. All work is done\n```\n\n## Custom QueueClass\n\nFor implementing more complex scheduling policies, you can provide a QueueClass in the options:\n\n```js\nimport PQueue from 'p-queue-cjs';\n\nclass QueueClass {\n\tconstructor() {\n\t\tthis._queue = [];\n\t}\n\n\tenqueue(run, options) {\n\t\tthis._queue.push(run);\n\t}\n\n\tdequeue() {\n\t\treturn this._queue.shift();\n\t}\n\n\tget size() {\n\t\treturn this._queue.length;\n\t}\n\n\tfilter(options) {\n\t\treturn this._queue;\n\t}\n}\n\nconst queue = new PQueue({queueClass: QueueClass});\n```\n\n`p-queue-cjs` will call corresponding methods to put and get operations from this queue.\n\n## FAQ\n\n#### How do the `concurrency` and `intervalCap` options affect each other?\n\nThey are just different constraints. The `concurrency` option limits how many things run at the same time. The `intervalCap` option limits how many things run in total during the interval (over time).\n\n## Maintainers\n\n- [Sindre Sorhus](https://github.com/sindresorhus)\n- [Richie Bendall](https://github.com/Richienb)\n\n## Related\n\n- [p-limit](https://github.com/sindresorhus/p-limit) - Run multiple promise-returning & async functions with limited concurrency\n- [p-throttle](https://github.com/sindresorhus/p-throttle) - Throttle promise-returning & async functions\n- [p-debounce](https://github.com/sindresorhus/p-debounce) - Debounce promise-returning & async functions\n- [p-all](https://github.com/sindresorhus/p-all) - Run promise-returning & async functions concurrently with optional limited concurrency\n- [More…](https://github.com/sindresorhus/promise-fun)\n\n---\n\n<div align=\"center\">\n\t<b>\n\t\t<a href=\"https://tidelift.com/subscription/pkg/npm-p-queue?utm_source=npm-p-queue&utm_medium=referral&utm_campaign=readme\">Get professional support for this package with a Tidelift subscription</a>\n\t</b>\n\t<br>\n\t<sub>\n\t\tTidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies.\n\t</sub>\n</div>\n","readmeFilename":"readme.md"}