Gathering detailed insights and metrics for throttled-queue-timeout
Gathering detailed insights and metrics for throttled-queue-timeout
Gathering detailed insights and metrics for throttled-queue-timeout
Gathering detailed insights and metrics for throttled-queue-timeout
Throttles arbitrary code to execute a maximum number of times per interval while using a timeout. Best for making throttled API requests.
npm install throttled-queue-timeout
Typescript
Module System
Node Version
NPM Version
TypeScript (85.55%)
JavaScript (14.45%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
46 Commits
2 Branches
1 Contributors
Updated on Oct 22, 2022
Latest Version
2.1.7
Package Id
throttled-queue-timeout@2.1.7
Unpacked Size
16.50 kB
Size
5.22 kB
File Count
9
NPM Version
8.15.0
Node Version
16.17.1
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-timeout
will not prevent any executions. Instead, every execution is placed into a queue, which will be drained at the desired rate limit.
Starting in version 2.1.5
, you can also add a timeout to the waiting. If the promise doesn't resolve before the timeout is reached, it will throw an Error("Cancelled due to timeout")
1npm install throttled-queue-timeout
It can be used in a Node.js environment, or directly in the browser.
require
or import
the factory function:1const throttledQueueTimeout = require('throttled-queue-timeout');
1import throttledQueueTimeout from 'throttled-queue-timeout';
1const throttle = throttledQueueTimeout(5, 1000); // 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.
1const throttledQueueTimeout = require('throttled-queue-timeout'); 2const throttle = throttledQueueTimeout(1, 1000); // at most make 1 request every second. 3 4for (let x = 0; x < 100; x++) { 5 throttle(() => { 6 // make a network request. 7 return fetch('https://api.github.com/search/users?q=Zhell1'); 8 }); 9}
Wherever the throttle
instance is used, your action will be placed into the same queue,
and be subject to the same rate limits.
1const throttledQueueTimeout = require('throttled-queue-timeout'); 2const throttle = throttledQueueTimeout(1, 60 * 1000); // at most make 1 request every minute. 3 4for (let x = 0; x < 50; x++) { 5 throttle(() => { 6 // make a network request. 7 return fetch('https://api.github.com/search/users?q=Zhell1'); 8 }); 9} 10for (let y = 0; y < 50; y++) { 11 throttle(() => { 12 // make another type of network request. 13 return fetch('https://api.github.com/search/repositories?q=throttled-queue+user:Zhell1'); 14 }); 15}
By specifying a number higher than 1 as the first parameter, you can dequeue multiple actions within the given interval:
1const throttledQueueTimeout = require('throttled-queue-timeout'); 2const throttle = throttledQueueTimeout(10, 1000); // at most make 10 requests every second. 3 4for (let x = 0; x < 100; x++) { 5 throttle(() => { 6 // This will fire at most 10 a second, as rapidly as possible. 7 return fetch('https://api.github.com/search/users?q=Zhell1'); 8 }); 9}
You can space out your actions by specifying true
as the third (optional) parameter:
1const throttledQueueTimeout = require('throttled-queue-timeout'); 2const throttle = throttledQueueTimeout(10, 1000, true); // at most make 10 requests every second, but evenly spaced. 3 4for (var x = 0; x < 100; x++) { 5 throttle(() => { 6 // This will fire at most 10 requests a second, spacing them out instead of in a burst. 7 return fetch('https://api.github.com/search/users?q=Zhell1'); 8 }); 9}
Starting in version 2.0.0
, you can wait for the results of your operation:
1const throttledQueueTimeout = require('throttled-queue-timeout'); 2const throttle = throttledQueueTimeout(10, 1000, true); // at most make 10 requests every second, but evenly spaced. 3 4const usernames = ['Zhell1', 'forward-motion']; 5const profiles = await Promise.all( 6 usernames.map((username) => throttle(() => { 7 return fetch(`https://api.github.com/search/users?q=${username}`); 8 })) 9); 10 11const justMe = await throttle(() => fetch('https://api.github.com/search/users?q=Zhell1'));
Starting in version 2.1.5
, you can also add a timeout to the waiting. If the promise doesn't resolve before the timeout is reached, it will throw an Error("Cancelled due to timeout")
1const imeout = require('throttled-queue-timeout'); 2// at most make 4 requests per minute, with timeout after 2 seconds 3const throttle = throttledQueueTimeout(4, 60*1000, false, 2000); 4 5for(let i=0; i<5; i++){ 6 try { 7 const res = await throttle(() => fetch('https://api.github.com/search/users?q=Zhell1')); 8 console.log("request",i,"success") 9 } 10 catch(error) { 11 console.log("request",i,"error:",error) 12 } 13}
The package is written in Typescript and includes types by default. The throttle
function is a generic,
and in most cases will automatically infer the right type for the result of the promise from the input.
However, you may also specify the return type of the promise when needed:
1import throttledQueueTimeout from 'throttled-queue-timeout'; 2const throttle = throttledQueueTimeout(1, 1000); 3const result1 = await throttle<string>(() => '1'); 4const result2 = await throttle<boolean>(() => Promise.resolve(true));
No vulnerabilities found.
No security vulnerabilities found.