Package Exports
- @henrygd/queue
Readme
@henrygd/queue
Tiny async queue with concurrency control. Like p-limit, queue, or promise-queue, but a lot smaller.
Installation
npm install @henrygd/queue
Usage
Add promises to the queue with the add
method. It returns a promise that resolves when the job is finished.
import { newQueue } from '@henrygd/queue'
// exmaple promise that waits for x milliseconds
const wait = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms))
// create a queue with a maximum of 5 concurrent jobs
const queue = newQueue(5)
// add 25 promises to the queue and log when they finish
for (let i = 1; i <= 25; i++) {
queue.add(() => wait(500)).then(() => console.log(i, 'done'))
}
The returned promise has the same type as the supplied promise, so use it as if you were using the supplied promise directly.
const queue = newQueue(2)
const sites = [
'https://google.com',
'https://github.com',
'https://youtube.com',
'https://twitter.com',
'https://facebook.com',
'https://reddit.com',
]
for (const site of sites) {
queue.add(() => fetch(site)).then((res) => console.log(site, res.status))
}