Comparing version 5.0.0 to 6.0.0
@@ -27,3 +27,3 @@ export type LimitFunction = { | ||
<Arguments extends unknown[], ReturnType>( | ||
fn: (...arguments_: Arguments) => PromiseLike<ReturnType> | ReturnType, | ||
function_: (...arguments_: Arguments) => PromiseLike<ReturnType> | ReturnType, | ||
...arguments_: Arguments | ||
@@ -30,0 +30,0 @@ ): Promise<ReturnType>; |
29
index.js
import Queue from 'yocto-queue'; | ||
import {AsyncResource} from '#async_hooks'; | ||
@@ -12,13 +11,17 @@ export default function pLimit(concurrency) { | ||
const next = () => { | ||
activeCount--; | ||
const resumeNext = () => { | ||
if (queue.size > 0) { | ||
queue.dequeue()(); | ||
// Since `pendingCount` has been decreased by one, increase `activeCount` by one. | ||
activeCount++; | ||
} | ||
}; | ||
const next = () => { | ||
activeCount--; | ||
resumeNext(); | ||
}; | ||
const run = async (function_, resolve, arguments_) => { | ||
activeCount++; | ||
const result = (async () => function_(...arguments_))(); | ||
@@ -36,4 +39,8 @@ | ||
const enqueue = (function_, resolve, arguments_) => { | ||
queue.enqueue( | ||
AsyncResource.bind(run.bind(undefined, function_, resolve, arguments_)), | ||
// Queue `internalResolve` instead of the `run` function | ||
// to preserve asynchronous context. | ||
new Promise(internalResolve => { | ||
queue.enqueue(internalResolve); | ||
}).then( | ||
run.bind(undefined, function_, resolve, arguments_), | ||
); | ||
@@ -44,8 +51,8 @@ | ||
// `activeCount` to `concurrency`, because `activeCount` is updated asynchronously | ||
// when the run function is dequeued and called. The comparison in the if-statement | ||
// after the `internalResolve` 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()(); | ||
if (activeCount < concurrency) { | ||
resumeNext(); | ||
} | ||
@@ -52,0 +59,0 @@ })(); |
{ | ||
"name": "p-limit", | ||
"version": "5.0.0", | ||
"version": "6.0.0", | ||
"description": "Run multiple promise-returning & async functions with limited concurrency", | ||
@@ -18,8 +18,3 @@ "license": "MIT", | ||
}, | ||
"imports": { | ||
"#async_hooks": { | ||
"node": "async_hooks", | ||
"default": "./async-hooks-stub.js" | ||
} | ||
}, | ||
"sideEffects": false, | ||
"engines": { | ||
@@ -33,4 +28,3 @@ "node": ">=18" | ||
"index.js", | ||
"index.d.ts", | ||
"async-hooks-stub.js" | ||
"index.d.ts" | ||
], | ||
@@ -55,6 +49,6 @@ "keywords": [ | ||
"dependencies": { | ||
"yocto-queue": "^1.0.0" | ||
"yocto-queue": "^1.1.1" | ||
}, | ||
"devDependencies": { | ||
"ava": "^5.3.1", | ||
"ava": "^6.1.3", | ||
"delay": "^6.0.0", | ||
@@ -64,5 +58,5 @@ "in-range": "^3.0.0", | ||
"time-span": "^5.1.0", | ||
"tsd": "^0.29.0", | ||
"xo": "^0.56.0" | ||
"tsd": "^0.31.1", | ||
"xo": "^0.58.0" | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
7654
5
94
Updatedyocto-queue@^1.1.1