p-queue
Promise queue with concurrency control
Useful for rate-limiting async operations. For example, when interacting with a REST API or when doing CPU/memory intensive tasks.
Install
$ npm install --save p-queue
Usage
Here we run only one promise at the time. For example, set concurrency
to 4 to run four promises at the time.
const PQueue = ;const got = ; const queue = concurrency: 1; queue; queue; ;
API
PQueue([options])
Returns a new queue
instance.
options
Type: Object
concurrency
Type: number
Default: Infinity
Minimum: 1
Concurrency limit.
queueClass
Type: Function
Class with a enqueue
and dequeue
method, and a size
getter. See the Custom QueueClass section.
queue
PQueue
instance.
.add(fn, [options])
Returns the promise returned by calling fn
.
fn
Type: Function
Promise-returning/async function.
options
Type: Object
priority
Type: number
Default: 0
Priority of operation. Operations with greater priority will be scheduled first.
.onEmpty()
Returns a promise that settles when the queue becomes empty.
Can be called multiple times. Useful if you for example add additional items at a later time.
.size
Size of the queue.
.pending
Number of pending promises.
Advanced example
A more advanced example to help you understand the flow.
const delay = ;const PQueue = ; const queue = concurrency: 1; ; queue;console; queue;console; queue; console;//=> '3. Queue size: 1`console;//=> '4. Pending promises: 1'
$ node example.js
1. Added 🦄
2. Added 🐴
3. Queue size: 1
4. Pending promises: 1
5. Resolved 🦄
6. Resolved 🐴
7. Queue is empty
8. Pending promises: 0
9. Added 🐙
10. Pending promises: 1
11. Resolved 🐙
12. Queue is empty again
Custom QueueClass
For implementing more complex scheduling policies, you can provide a QueueClass in the options:
{ this_queue = ; } { this_queue; } { return this_queue; } { return this_queuelength; }
p-queue
will call corresponding methods to put and get operations from this queue.
Related
- p-limit - Run multiple promise-returning & async functions with limited concurrency
- p-throttle - Throttle promise-returning & async functions
- p-debounce - Debounce promise-returning & async functions
- p-all - Run promise-returning & async functions concurrently with optional limited concurrency
- More…
Created by
License
MIT © Sindre Sorhus