pax_global_header00006660000000000000000000000064141053326010014505gustar00rootroot0000000000000052 comment=38a6773e552d24f4c9eb2d79d57b6cff017c587d p-limit-4.0.0/000077500000000000000000000000001410533260100130615ustar00rootroot00000000000000p-limit-4.0.0/.editorconfig000066400000000000000000000002571410533260100155420ustar00rootroot00000000000000root = true [*] indent_style = tab end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true [*.yml] indent_style = space indent_size = 2 p-limit-4.0.0/.gitattributes000066400000000000000000000000231410533260100157470ustar00rootroot00000000000000* text=auto eol=lf p-limit-4.0.0/.github/000077500000000000000000000000001410533260100144215ustar00rootroot00000000000000p-limit-4.0.0/.github/funding.yml000066400000000000000000000001611410533260100165740ustar00rootroot00000000000000github: sindresorhus open_collective: sindresorhus tidelift: npm/p-limit custom: https://sindresorhus.com/donate p-limit-4.0.0/.github/security.md000066400000000000000000000002631410533260100166130ustar00rootroot00000000000000# Security Policy To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure. p-limit-4.0.0/.github/workflows/000077500000000000000000000000001410533260100164565ustar00rootroot00000000000000p-limit-4.0.0/.github/workflows/main.yml000066400000000000000000000006261410533260100201310ustar00rootroot00000000000000name: CI on: - push - pull_request jobs: test: name: Node.js ${{ matrix.node-version }} runs-on: ubuntu-latest strategy: fail-fast: false matrix: node-version: - 16 steps: - uses: actions/checkout@v2 - uses: actions/setup-node@v2 with: node-version: ${{ matrix.node-version }} - run: npm install - run: npm test p-limit-4.0.0/.gitignore000066400000000000000000000000271410533260100150500ustar00rootroot00000000000000node_modules yarn.lock p-limit-4.0.0/.npmrc000066400000000000000000000000231410533260100141740ustar00rootroot00000000000000package-lock=false p-limit-4.0.0/index.d.ts000066400000000000000000000026221410533260100147640ustar00rootroot00000000000000/* eslint-disable @typescript-eslint/member-ordering */ export interface LimitFunction { /** The number of promises that are currently running. */ readonly activeCount: number; /** The number of promises that are waiting to run (i.e. their internal `fn` was not called yet). */ readonly pendingCount: number; /** Discard pending promises that are waiting to run. This might be useful if you want to teardown the queue at the end of your program's lifecycle or discard any function calls referencing an intermediary state of your app. Note: This does not cancel promises that are already running. */ clearQueue: () => void; /** @param fn - Promise-returning/async function. @param arguments - Any arguments to pass through to `fn`. Support for passing arguments on to the `fn` is provided in order to be able to avoid creating unnecessary closures. You probably don't need this optimization unless you're pushing a lot of functions. @returns The promise returned by calling `fn(...arguments)`. */ ( fn: (...arguments: Arguments) => PromiseLike | ReturnType, ...arguments: Arguments ): Promise; } /** Run multiple promise-returning & async functions with limited concurrency. @param concurrency - Concurrency limit. Minimum: `1`. @returns A `limit` function. */ export default function pLimit(concurrency: number): LimitFunction; p-limit-4.0.0/index.js000066400000000000000000000027361410533260100145360ustar00rootroot00000000000000import Queue from 'yocto-queue'; export default function pLimit(concurrency) { if (!((Number.isInteger(concurrency) || concurrency === Number.POSITIVE_INFINITY) && concurrency > 0)) { throw new TypeError('Expected `concurrency` to be a number from 1 and up'); } const queue = new Queue(); let activeCount = 0; const next = () => { activeCount--; if (queue.size > 0) { queue.dequeue()(); } }; const run = async (fn, resolve, args) => { activeCount++; const result = (async () => fn(...args))(); resolve(result); try { await result; } catch {} next(); }; const enqueue = (fn, resolve, args) => { queue.enqueue(run.bind(undefined, fn, resolve, args)); (async () => { // This function needs to wait until the next microtask before comparing // `activeCount` to `concurrency`, because `activeCount` is updated asynchronously // when the run function is dequeued and called. The comparison in the if-statement // needs to happen asynchronously as well to get an up-to-date value for `activeCount`. await Promise.resolve(); if (activeCount < concurrency && queue.size > 0) { queue.dequeue()(); } })(); }; const generator = (fn, ...args) => new Promise(resolve => { enqueue(fn, resolve, args); }); Object.defineProperties(generator, { activeCount: { get: () => activeCount, }, pendingCount: { get: () => queue.size, }, clearQueue: { value: () => { queue.clear(); }, }, }); return generator; } p-limit-4.0.0/index.test-d.ts000066400000000000000000000010261410533260100157360ustar00rootroot00000000000000import {expectType} from 'tsd'; import pLimit from './index.js'; const limit = pLimit(1); const input = [ limit(async () => 'foo'), limit(async () => 'bar'), limit(async () => undefined), ]; expectType>>(Promise.all(input)); expectType>(limit((_a: string) => '', 'test')); expectType>(limit(async (_a: string, _b: number) => '', 'test', 1)); expectType(limit.activeCount); expectType(limit.pendingCount); expectType(limit.clearQueue()); p-limit-4.0.0/license000066400000000000000000000021351410533260100144270ustar00rootroot00000000000000MIT License Copyright (c) Sindre Sorhus (https://sindresorhus.com) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. p-limit-4.0.0/package.json000066400000000000000000000017521410533260100153540ustar00rootroot00000000000000{ "name": "p-limit", "version": "4.0.0", "description": "Run multiple promise-returning & async functions with limited concurrency", "license": "MIT", "repository": "sindresorhus/p-limit", "funding": "https://github.com/sponsors/sindresorhus", "author": { "name": "Sindre Sorhus", "email": "sindresorhus@gmail.com", "url": "https://sindresorhus.com" }, "type": "module", "exports": "./index.js", "engines": { "node": "^12.20.0 || ^14.13.1 || >=16.0.0" }, "scripts": { "test": "xo && ava && tsd" }, "files": [ "index.js", "index.d.ts" ], "keywords": [ "promise", "limit", "limited", "concurrency", "throttle", "throat", "rate", "batch", "ratelimit", "task", "queue", "async", "await", "promises", "bluebird" ], "dependencies": { "yocto-queue": "^1.0.0" }, "devDependencies": { "ava": "^3.15.0", "delay": "^5.0.0", "in-range": "^3.0.0", "random-int": "^3.0.0", "time-span": "^5.0.0", "tsd": "^0.17.0", "xo": "^0.44.0" } } p-limit-4.0.0/readme.md000066400000000000000000000052651410533260100146500ustar00rootroot00000000000000# p-limit > Run multiple promise-returning & async functions with limited concurrency ## Install ``` $ npm install p-limit ``` ## Usage ```js import pLimit from 'p-limit'; const limit = pLimit(1); const input = [ limit(() => fetchSomething('foo')), limit(() => fetchSomething('bar')), limit(() => doSomething()) ]; // Only one promise is run at once const result = await Promise.all(input); console.log(result); ``` ## API ### pLimit(concurrency) Returns a `limit` function. #### concurrency Type: `number`\ Minimum: `1`\ Default: `Infinity` Concurrency limit. ### limit(fn, ...args) Returns the promise returned by calling `fn(...args)`. #### fn Type: `Function` Promise-returning/async function. #### args Any arguments to pass through to `fn`. Support for passing arguments on to the `fn` is provided in order to be able to avoid creating unnecessary closures. You probably don't need this optimization unless you're pushing a *lot* of functions. ### limit.activeCount The number of promises that are currently running. ### limit.pendingCount The number of promises that are waiting to run (i.e. their internal `fn` was not called yet). ### limit.clearQueue() Discard pending promises that are waiting to run. This might be useful if you want to teardown the queue at the end of your program's lifecycle or discard any function calls referencing an intermediary state of your app. Note: This does not cancel promises that are already running. ## FAQ ### How is this different from the [`p-queue`](https://github.com/sindresorhus/p-queue) package? This package is only about limiting the number of concurrent executions, while `p-queue` is a fully featured queue implementation with lots of different options, introspection, and ability to pause the queue. ## Related - [p-queue](https://github.com/sindresorhus/p-queue) - Promise queue with concurrency control - [p-throttle](https://github.com/sindresorhus/p-throttle) - Throttle promise-returning & async functions - [p-debounce](https://github.com/sindresorhus/p-debounce) - Debounce promise-returning & async functions - [p-all](https://github.com/sindresorhus/p-all) - Run promise-returning & async functions concurrently with optional limited concurrency - [More…](https://github.com/sindresorhus/promise-fun) ---
Get professional support for this package with a Tidelift subscription
Tidelift helps make open source sustainable for maintainers while giving companies
assurances about security, maintenance, and licensing for their dependencies.
p-limit-4.0.0/test.js000066400000000000000000000062501410533260100144010ustar00rootroot00000000000000import test from 'ava'; import delay from 'delay'; import inRange from 'in-range'; import timeSpan from 'time-span'; import randomInt from 'random-int'; import pLimit from './index.js'; test('concurrency: 1', async t => { const input = [ [10, 300], [20, 200], [30, 100], ]; const end = timeSpan(); const limit = pLimit(1); const mapper = ([value, ms]) => limit(async () => { await delay(ms); return value; }); t.deepEqual(await Promise.all(input.map(x => mapper(x))), [10, 20, 30]); t.true(inRange(end(), {start: 590, end: 650})); }); test('concurrency: 4', async t => { const concurrency = 5; let running = 0; const limit = pLimit(concurrency); const input = Array.from({length: 100}, () => limit(async () => { running++; t.true(running <= concurrency); await delay(randomInt(30, 200)); running--; })); await Promise.all(input); }); test('non-promise returning function', async t => { await t.notThrowsAsync(async () => { const limit = pLimit(1); await limit(() => null); }); }); test('continues after sync throw', async t => { const limit = pLimit(1); let ran = false; const promises = [ limit(() => { throw new Error('err'); }), limit(() => { ran = true; }), ]; await Promise.all(promises).catch(() => {}); t.is(ran, true); }); test('accepts additional arguments', async t => { const limit = pLimit(1); const symbol = Symbol('test'); await limit(a => t.is(a, symbol), symbol); }); test('does not ignore errors', async t => { const limit = pLimit(1); const error = new Error('🦄'); const promises = [ limit(async () => { await delay(30); }), limit(async () => { await delay(80); throw error; }), limit(async () => { await delay(50); }), ]; await t.throwsAsync(Promise.all(promises), {is: error}); }); test('activeCount and pendingCount properties', async t => { const limit = pLimit(5); t.is(limit.activeCount, 0); t.is(limit.pendingCount, 0); const runningPromise1 = limit(() => delay(1000)); t.is(limit.activeCount, 0); t.is(limit.pendingCount, 1); await Promise.resolve(); t.is(limit.activeCount, 1); t.is(limit.pendingCount, 0); await runningPromise1; t.is(limit.activeCount, 0); t.is(limit.pendingCount, 0); const immediatePromises = Array.from({length: 5}, () => limit(() => delay(1000))); const delayedPromises = Array.from({length: 3}, () => limit(() => delay(1000))); await Promise.resolve(); t.is(limit.activeCount, 5); t.is(limit.pendingCount, 3); await Promise.all(immediatePromises); t.is(limit.activeCount, 3); t.is(limit.pendingCount, 0); await Promise.all(delayedPromises); t.is(limit.activeCount, 0); t.is(limit.pendingCount, 0); }); test('clearQueue', async t => { const limit = pLimit(1); Array.from({length: 1}, () => limit(() => delay(1000))); Array.from({length: 3}, () => limit(() => delay(1000))); await Promise.resolve(); t.is(limit.pendingCount, 3); limit.clearQueue(); t.is(limit.pendingCount, 0); }); test('throws on invalid concurrency argument', t => { t.throws(() => { pLimit(0); }); t.throws(() => { pLimit(-1); }); t.throws(() => { pLimit(1.2); }); t.throws(() => { pLimit(undefined); }); t.throws(() => { pLimit(true); }); });