Gathering detailed insights and metrics for throttled-queue
Gathering detailed insights and metrics for throttled-queue
Gathering detailed insights and metrics for throttled-queue
Gathering detailed insights and metrics for throttled-queue
ak-throttled-queue
throttled queue for async functions
atlas-throttled-queue
Async job queue that limits the rate of job execution.
throttled-queue-timeout
Throttles arbitrary code to execute a maximum number of times per interval with a timeout. Best for making throttled API requests.
dynamic-throttled-queue
Forked from shaunpersad/throttled-queue. Dynamically throttles arbitrary code to execute a minimum/maximum number of times per interval. Best for making throttled API requests.
Throttles arbitrary code to execute a maximum number of times per interval. Best for making throttled API requests.
npm install throttled-queue
Typescript
Module System
Node Version
NPM Version
99.2
Supply Chain
99.5
Quality
82
Maintenance
100
Vulnerability
100
License
TypeScript (95.81%)
JavaScript (4.19%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
122 Stars
47 Commits
22 Forks
1 Watchers
2 Branches
2 Contributors
Updated on Jul 02, 2025
Latest Version
3.0.0
Package Id
throttled-queue@3.0.0
Unpacked Size
60.09 kB
Size
10.08 kB
File Count
13
NPM Version
9.5.1
Node Version
18.16.0
Published on
Jul 02, 2025
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
Throttles arbitrary code to execute a maximum number of times per interval. Best for making throttled API requests.
For example, making network calls to popular APIs such as Twitter is subject to rate limits. By wrapping all of your API calls in a throttle, it will automatically adjust your requests to be within the acceptable rate limits.
Unlike the throttle
functions of popular libraries like lodash and underscore, throttled-queue
will not prevent any executions. Instead, every execution is placed into a queue, which will be drained at the desired rate limit.
1npm install throttled-queue
It can be used in a Node.js environment, or directly in the browser.
Note that version 3 is a breaking change:
import { throttledQueue } from 'throtted-queue
instead.Version 2.x
and lower:
1import throttledQueue from 'throttled-queue'; 2const throttle = throttledQueue(5, 1000, true);
To upgrade to 3.x
, the following is equivalent to the above:
1import { throttledQueue, seconds } from 'throtted-queue'; 2const throttle = throttledQueue({ 3 maxPerInterval: 5, 4 interval: seconds(1), // you can still pass in the milliseconds directly, i.e. 1000 5 evenlySpaced: true, 6});
import
the factory function:1import { throttledQueue } from 'throttled-queue';
CommonJS require
is also supported:
1const { throttledQueue } = require('throttled-queue');
1const throttle = throttledQueue({ 2 maxPerInterval: 5, 3 interval: 1000, 4}); // at most 5 requests per second.
You may also use seconds
, minutes
, and hours
helpers to calculate the interval duration:
1import { throttledQueue, seconds } from 'throtted-queue'; 2const throttle = throttledQueue({ 3 maxPerInterval: 5, 4 interval: seconds(1), 5}); // at most 5 requests per second.
throttle
instance as a function to enqueue actions:1throttle(() => { 2 // perform some type of activity in here. 3});
The throttle
function will also return a promise with the result of your operation:
1const result = await throttle(() => { 2 return Promise.resolve('hello!'); 3}); 4// result now equals "hello"
Rapidly assigning network calls to be run, but they will be limited to 1 request per second.
1import { throttledQueue, seconds } from 'throttled-queue'; 2const throttle = throttledQueue({ 3 maxPerInterval: 1, 4 interval: seconds(1), 5}); // at most make 1 request every second. 6 7for (let x = 0; x < 100; x++) { 8 throttle(() => { 9 // make a network request. 10 return fetch('https://api.github.com/search/users?q=shaunpersad'); 11 }); 12}
Wherever the throttle
instance is used, your action will be placed into the same queue,
and be subject to the same rate limits.
1import { throttledQueue, minutes } from 'throttled-queue'; 2const throttle = throttledQueue({ 3 maxPerInterval: 1, 4 interval: minutes(1), 5}); // at most make 1 request every minute. 6 7for (let x = 0; x < 50; x++) { 8 throttle(() => { 9 // make a network request. 10 return fetch('https://api.github.com/search/users?q=shaunpersad'); 11 }); 12} 13for (let y = 0; y < 50; y++) { 14 throttle(() => { 15 // make another type of network request. 16 return fetch('https://api.github.com/search/repositories?q=throttled-queue+user:shaunpersad'); 17 }); 18}
Uou can perform multiple executions within the given interval:
1import { throttledQueue, seconds } from 'throttled-queue'; 2const throttle = throttledQueue({ 3 maxPerInterval: 10, 4 interval: seconds(1), 5}); // at most make 10 requests every second. 6 7for (let x = 0; x < 100; x++) { 8 throttle(() => { 9 // This will fire at most 10 a second, as rapidly as possible. 10 return fetch('https://api.github.com/search/users?q=shaunpersad'); 11 }); 12}
You can space out your actions by specifying true
as the third (optional) parameter:
1import { throttledQueue, seconds } from 'throttled-queue'; 2const throttle = throttledQueue({ 3 maxPerInterval: 10, 4 interval: seconds(1), 5 evenlySpaced: true, 6}) 7 8for (var x = 0; x < 100; x++) { 9 throttle(() => { 10 // This will fire at most 10 requests a second, spacing them out instead of in a burst. 11 return fetch('https://api.github.com/search/users?q=shaunpersad'); 12 }); 13}
You can also wait for the results of your operation:
1import { throttledQueue, seconds } from 'throttled-queue'; 2const throttle = throttledQueue({ 3 maxPerInterval: 10, 4 interval: seconds(1), 5}); 6 7const usernames = ['shaunpersad', 'forward-motion']; 8const profiles = await Promise.all( 9 usernames.map((username) => throttle(() => { 10 return fetch(`https://api.github.com/search/users?q=${username}`); 11 })) 12); 13 14const justMe = await throttle(() => fetch('https://api.github.com/search/users?q=shaunpersad'));
Starting in version 3.0.0
, you can now retry individual executions, or pause the queue entirely until a cooldown.
Both are useful for reacting to different status codes when calling an API.
To pause and/or retry executions, you can throw the new RetryError
:
1import { throttledQueue, seconds, RetryError } from 'throttled-queue'; 2const throttle = throttledQueue({ 3 maxPerInterval: 10, 4 interval: seconds(1), 5}); 6const result = await throttle(async () => { 7 const response = await fetch('https://api.github.com/search/users?q=shaunpersad'); 8 if (response.status === 429) { 9 const retryAfter = response.headers.get('retry-after'); 10 if (retryAfter) { // retry-after is in seconds 11 throw new RetryError({ 12 retryAfter: seconds(retryAfter), 13 pauseQueue: true, 14 }); // pause the queue until retryAfter 15 } 16 // if we can't tell when to retry, wait for the given interval of 1 second 17 throw new RetryError({ 18 pauseQueue: true, 19 }); 20 } 21 // for all other bad statuses, we just want to retry this specific API call 22 if (!response.ok) { 23 throw new RetryError(); 24 } 25 // if the response succeeded, return the result 26 return response.json(); 27});
RetryError
optionsAny of the following options can be optionally passed in when constructing a RetryError
:
retryAfter
: A number in milliseconds describing how long to wait to retry. If it is not set, or is set to null
, it defaults to the interval
set in the throttledQueue
options. If both retryAfter
and interval
are not set, it defaults to DEFAULT_WAIT
, which is currently 500 milliseconds.pauseQueue
: If set to true
, will pause the entire queue's execution. Note that it does not immediately pause all executions already in-flight, but subsequent executions will be paused. The queue will be paused by the amount of time specified by retryAfter
.message
: An error message to attach to the error object.By default, each category is limited to a maximum of 30 (DEFAULT_RETRY_LIMIT
) retries.
You can override this limit for both retries and retries with queue pauses in the options passed to throttledQueue
:
maxRetries
option applies only to calls where RetryError
is thrown with pauseQueue: false
, or not set.maxRetriesWithPauses
option applies only to calls where RetryError
is thrown with pauseQueue: true
.If the maximum number of retries is exceeded, the RetryError
will be thrown.
Using RetryError
, you can define queues that are unbounded, meaning their rate limit is not initially defined. You can then pause the queue once your underlying API returns an error:
1import { throttledQueue, seconds, RetryError } from 'throttled-queue'; 2const throttle = throttledQueue(); // passing no options creates an "unbounded" queue, executing as fast as possible 3const result = await throttle(async () => { 4 const response = await fetch('https://api.github.com/search/users?q=shaunpersad'); 5 if (response.status === 429) { 6 const retryAfter = response.headers.get('retry-after'); 7 throw new RetryError({ 8 retryAfter: retryAfter ? seconds(retryAfter) : null, 9 pauseQueue: true, 10 }); // pause the queue until retryAfter 11 } 12 // for all other bad statuses, we just want to retry this specific API call 13 if (!response.ok) { 14 throw new RetryError(); 15 } 16 // if the response succeeded, return the result 17 return response.json(); 18});
The second argument of the throttle
enqueue function accepts an arbitrary object that will be passed on to the execution context (the first argument) of the function being enqueued:
1const badStatuses = []; 2const result = await throttle( 3 async ({ state }) => { 4 const response = await fetch('https://api.github.com/search/users?q=shaunpersad'); 5 if (!response.ok) { 6 state.badStatuses.push(response.status); 7 throw new RetryError(); 8 } 9 return response.json(); 10 }, 11 { badStatuses }, // this object be available across all retries of the same enqueued function above 12); 13console.log(badStatuses); // this array now contains a log of all bad statuses received.
Note that you can pass any object as the initial state, so if you wanted to keep track of the number of retries, or implement more advanced retries using exponential backoff etc., you could store whatever you needed to in the state object.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
license file detected
Details
Reason
2 commit(s) and 2 issue activity found in the last 90 days -- score normalized to 3
Reason
Found 1/27 approved changesets -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
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
Reason
10 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-07-07
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