Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/alextes/run_with_limit
Run promise-returning & async functions with limited concurrency
https://github.com/alextes/run_with_limit
Last synced: 2 days ago
JSON representation
Run promise-returning & async functions with limited concurrency
- Host: GitHub
- URL: https://github.com/alextes/run_with_limit
- Owner: alextes
- Created: 2020-05-22T13:01:30.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2021-06-25T16:16:29.000Z (over 3 years ago)
- Last Synced: 2024-12-22T19:24:15.265Z (15 days ago)
- Language: TypeScript
- Homepage:
- Size: 14.6 KB
- Stars: 0
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# Run with limit
Run promise-returning & async functions with limited concurrency. Heavily
inspired by [sindresorhus/p-limit](https://github.com/sindresorhus/p-limit).## Usage
```ts
import { makeRunWithLimit } from "https://denopkg.com/alextes/run-with-limit";const { runWithLimit } = makeRunWithLimit(1);
(async () => {
// Only one promise is run at once
const result = await Promise.all([
runWithLimit(() => fetchSomething("foo")),
runWithLimit(() => fetchSomething("bar")),
runWithLimit(() => doSomething()),
]);
console.log(result);
})();
```## API
### makeRunWithLimit(concurrency)
`Number => { runWithLimit, getActiveCount, getPendingCount }`
Takes a number, setting the concurrency for the promise queue. Returns a set of
functions to use the queue.#### runWithLimit
Pass a thunk to this function that returns a promise.
That is, pass a function which when invoked, kicks off an asynchronous
computation, and if you need its result, returns a promise. `runWithLimit` will
resolve with said result while honoring the concurrency limit.#### getActiveCount
`() => Number`
Call to get the number of promises that are currently running.
#### getPendingCount
`() => Number`
Call to check how many promises are still waiting to start execution.