Gathering detailed insights and metrics for async-await-queue
Gathering detailed insights and metrics for async-await-queue
Gathering detailed insights and metrics for async-await-queue
Gathering detailed insights and metrics for async-await-queue
npm install async-await-queue
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
58 Stars
360 Commits
2 Forks
2 Watching
8 Branches
2 Contributors
Updated on 28 Nov 2024
TypeScript (72.7%)
JavaScript (27.3%)
Cumulative downloads
Total Downloads
Last day
24.7%
2,115
Compared to previous day
Last week
-15.1%
10,051
Compared to previous week
Last month
4.1%
66,227
Compared to previous month
Last year
21.7%
424,249
Compared to previous year
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:
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).
2024 Update: My technique is on track to become an official ECMAScript language: ES Promise.withResolvers
npm install --save async-await-queue
Require as CJS
1const { Queue } = require('async-await-queue');
Import as ES6 Module
1import { 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.
1const { Queue } = require('async-await-queue'); 2/** 3 * No more than 2 concurrent tasks with 4 * at least 100ms between two tasks 5 * (measured from task start to task start) 6 */ 7const myq = new Queue(2, 100); 8const myPriority = -1; 9 10/** 11 * This function will launch all tasks and will 12 * wait for them to be scheduled, returning 13 * only when all tasks have finished 14 */ 15async function downloadTheInternet() { 16 for (let site of Internet) { 17 /** 18 * The third call will wait for the previous two to complete 19 * plus the time needed to make this at least 100ms 20 * after the second call 21 * The first argument needs to be unique for every 22 * task on the queue 23 */ 24 const me = Symbol(); 25 /* We wait in the line here */ 26 await myq.wait(me, myPriority); 27 28 /** 29 * Do your expensive async task here 30 * Queue will schedule it at 31 * no more than 2 requests running in parallel 32 * launched at least 100ms apart 33 */ 34 download(site) 35 /* Signal that we are finished */ 36 /* Do not forget to handle the exceptions! */ 37 .catch((e) => console.error(e)) 38 .finally(() => myq.end(me)); 39 } 40 return await myq.flush(); 41}
1/** 2 * This is the new style API introduced in 1.2 3 * It is equivalent to the previous example 4 */ 5async function downloadTheInternet() { 6 const q = []; 7 for (let site of Internet) { 8 /** The third call will wait for the previous two to complete 9 * plus the time needed to make this at least 100ms 10 * after the second call 11 */ 12 q.push(myq.run(() => download(site).catch((e) => console.error(e)))); 13 } 14 return Promise.all(q); 15}
1/** 2 * This function will execute a single task at a time 3 * waiting for its place in the queue 4 */ 5async function downloadTheInternet() { 6 let p; 7 /** 8 * The third call will wait for the previous two to complete 9 * plus the time needed to make this at least 100ms 10 * after the second call 11 * The first argument needs to be unique for every 12 * task on the queue 13 */ 14 const me = Symbol(); 15 /* We wait in the line here */ 16 await myq.wait(me, myPriority); 17 18 /** 19 * Do your expensive async task here 20 * Queue will schedule it at 21 * no more than 2 requests running in parallel 22 * launched at least 100ms apart 23 */ 24 try { 25 await download(site); 26 } catch (e) { 27 console.error(e); 28 } finally { 29 /* Signal that we are finished */ 30 /* Do not forget to handle the exceptions! */ 31 myq.end(me); 32 } 33}
1/** 2 * This function will schedule all the tasks and 3 * then will return immediately a single Promise 4 * that can be awaited upon 5 */ 6async function downloadTheInternet() { 7 const q = []; 8 for (let site of Internet) { 9 /** 10 * The third call will wait for the previous two to complete 11 * plus the time needed to make this at least 100ms 12 * after the second call 13 * The first argument needs to be unique for every 14 * task on the queue 15 */ 16 const me = Symbol(); 17 q.push( 18 myq 19 .wait(me, myPriority) 20 .then(() => download(site)) 21 .catch((e) => console.error(e)) 22 .finally(() => myq.end(me)) 23 ); 24 } 25 return Promise.all(q); 26}
Promise
s in Node.jsWhen 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
.
No vulnerabilities found.
Reason
30 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 10
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
2 existing vulnerabilities detected
Details
Reason
dependency not pinned by hash detected -- score normalized to 3
Details
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More