-
-
Notifications
You must be signed in to change notification settings - Fork 108
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Limit function #88
Limit function #88
Changes from 2 commits
0146915
6d74015
cf8a05d
aa4c59b
7c87383
b7d0472
1596c9b
0fb0b78
d9d6c41
45f90ac
72cadc8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -41,3 +41,22 @@ Run multiple promise-returning & async functions with limited concurrency. | |||||||||||||||||||||||
@returns A `limit` function. | ||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||
export default function pLimit(concurrency: number): LimitFunction; | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
export type LimitFunctionOption = { | ||||||||||||||||||||||||
/** | ||||||||||||||||||||||||
Concurrency limit | ||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||
concurrency: number; | ||||||||||||||||||||||||
}; | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
/** | ||||||||||||||||||||||||
Return a function with limited concurrency | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
@param function_ - Promise-returning/async function. | ||||||||||||||||||||||||
@param option | ||||||||||||||||||||||||
@return Function with limited concurrency | ||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||
export function limitFunction<Arguments extends unknown[], ReturnType>( | ||||||||||||||||||||||||
function_: (...arguments_: Arguments) => PromiseLike<ReturnType> | ReturnType, | ||||||||||||||||||||||||
option: LimitFunctionOption | ||||||||||||||||||||||||
): (...arguments_: Arguments) => Promise<ReturnType>; |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -28,6 +28,19 @@ const result = await Promise.all(input); | |||||
console.log(result); | ||||||
``` | ||||||
|
||||||
```js | ||||||
import {limitFunction} from 'p-limit'; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move this example to the limitFunction API docs. |
||||||
|
||||||
const limitFunction_ = limitFunction(async () => { | ||||||
return doSomething(); | ||||||
}, {concurrency: 1}); | ||||||
|
||||||
const input = Array.from({length: 10}, limitFunction_); | ||||||
|
||||||
// Only one promise is run at once | ||||||
await Promise.all(input); | ||||||
``` | ||||||
|
||||||
## API | ||||||
|
||||||
### pLimit(concurrency) | ||||||
|
@@ -77,6 +90,28 @@ Note: This does not cancel promises that are already running. | |||||
|
||||||
Get or set the concurrency limit. | ||||||
|
||||||
### limitFunction(fn, options) | ||||||
|
||||||
Return a function with limited concurrency | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
This needs to clearly describe how it's different from the main export. |
||||||
|
||||||
#### fn | ||||||
|
||||||
Type: `Function` | ||||||
|
||||||
Promise-returning/async function. | ||||||
|
||||||
#### options | ||||||
|
||||||
Type: `object` | ||||||
|
||||||
#### concurrency | ||||||
|
||||||
Type: `number`(Interget) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Typo |
||||||
|
||||||
Minimum: `1` | ||||||
|
||||||
#### option | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Leftover |
||||||
|
||||||
## FAQ | ||||||
|
||||||
### How is this different from the [`p-queue`](https://github.com/sindresorhus/p-queue) package? | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Plural
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, just name it
Options
as I would like to makepLimit
support an options-object too, for readability.