async-await-queue
Promise-based priority queues for throttling, rate- and concurrency limiting of Node.js or browser tasks
Zero-dependency, total size: 2.93 kB
uncompressed and 1.16 kB
gzip-compressed
There is a medium story about using this package to parallelize download loops : Parallelizing download loops in JS with async-await-queue
This is an interesting solution to the priority queues problem.
There are other Promise-based queues out there but they are not async/await compatible and do not support priorities.
It guarantees order and never wakes up contexts that won't run.
I use it with tens of thousands of jobs on the queue. O(log(n)) on the number of jobs, O(log(n)) on the number of different priorities. Just make sure to always call Queue.end()
. Or, since 1.2, there is a safer, but less versatile method, Queue.run()
.
Typical uses:
- Rate-limit expensive external API requests - especially on ban-happy servers
- Avoiding to launch all the tasks in an async loop at the same time while allowing some degree of controlled concurrency
The queues keep references to the Promise resolve()
function and resolve it from outside of the Promise constructor.
This is a very unusual use of Promises to implement locks that I find interesting (this is what the medium story is about).
Install
npm install --save async-await-queue
Typical usage
Require as CJS
const { Queue } = require('async-await-queue');
Import as ES6 Module
import { Queue } from 'async-await-queue';
(or read the jsdoc)
IMPORTANT Keep in mind that when running asynchronous code without explicitly await
ing it, you should always handle the eventual Promise rejections by a .catch()
statement.
Examples
Basic example
const { Queue } = require('async-await-queue');
const myq = new Queue(2, 100);
const myPriority = -1;
async function downloadTheInternet() {
for (let site of Internet) {
const me = Symbol();
await myq.wait(me, myPriority);
download(site)
.catch((e) => console.error(e))
.finally(() => myq.end(me));
}
return await myq.flush();
}
Using a function
async function downloadTheInternet() {
const q = [];
for (let site of Internet) {
q.push(myq.run(() => download(site).catch((e) => console.error(e))));
}
return Promise.all(q);
}
Running sequentially
async function downloadTheInternet() {
let p;
const me = Symbol();
await myq.wait(me, myPriority);
try {
await download(site);
} catch (e) {
console.error(e);
} finally {
myq.end(me);
}
}
Fire-and-forget
async function downloadTheInternet() {
const q = [];
for (let site of Internet) {
const me = Symbol();
q.push(
myq
.wait(me, myPriority)
.then(() => download(site))
.catch((e) => console.error(e))
.finally(() => myq.end(me))
);
}
return Promise.all(q);
}
Unresolvable Promise
s in Node.js
When using this package, something that you should be aware of is that Node.js has a very particular behavior when dealing with unresolvable Promise
s:
https://github.com/nodejs/node/issues/43162
When await
ing an unresolvable Promise
, Node.js will simply exit - instead of blocking indefinitely - which would probably be what most people expect.
If you are using this package in Node.js and it seems to simply unexpectedly exit without reaching the program's normal end and without reporting any errors, you most probably have an unresolvable Promise
.