npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

throttled-queue-timeout

v2.1.7

Published

Throttles arbitrary code to execute a maximum number of times per interval with a timeout. Best for making throttled API requests.

Downloads

3

Readme

throttled-queue-timeout

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")

Installation

npm install throttled-queue-timeout

It can be used in a Node.js environment, or directly in the browser.

Usage

  1. require or import the factory function:
const throttledQueueTimeout = require('throttled-queue-timeout');
import throttledQueueTimeout from 'throttled-queue-timeout';
  1. Create an instance of a throttled queue by specifying the maximum number of requests as the first parameter, and the interval in milliseconds as the second:
const throttle = throttledQueueTimeout(5, 1000); // at most 5 requests per second.
  1. Use the throttle instance as a function to enqueue actions:
throttle(() => {
    // perform some type of activity in here.
});

The throttle function will also return a promise with the result of your operation:

const result = await throttle(() => {
    return Promise.resolve('hello!');
});
// result now equals "hello"

Quick Examples

Basic

Rapidly assigning network calls to be run, but they will be limited to 1 request per second.

const throttledQueueTimeout = require('throttled-queue-timeout');
const throttle = throttledQueueTimeout(1, 1000); // at most make 1 request every second.

for (let x = 0; x < 100; x++) {
    throttle(() => {
        // make a network request.
        return fetch('https://api.github.com/search/users?q=Zhell1');
    });
}

Reusable

Wherever the throttle instance is used, your action will be placed into the same queue, and be subject to the same rate limits.

const throttledQueueTimeout = require('throttled-queue-timeout');
const throttle = throttledQueueTimeout(1, 60 * 1000); // at most make 1 request every minute.

for (let x = 0; x < 50; x++) {
    throttle(() => {
        // make a network request.
        return fetch('https://api.github.com/search/users?q=Zhell1');
    });
}
for (let y = 0; y < 50; y++) {
    throttle(() => {
        // make another type of network request.
        return fetch('https://api.github.com/search/repositories?q=throttled-queue+user:Zhell1');
    });
}

Bursts

By specifying a number higher than 1 as the first parameter, you can dequeue multiple actions within the given interval:

const throttledQueueTimeout = require('throttled-queue-timeout');
const throttle = throttledQueueTimeout(10, 1000); // at most make 10 requests every second.

for (let x = 0; x < 100; x++) {
    throttle(() => {
        // This will fire at most 10 a second, as rapidly as possible.
        return fetch('https://api.github.com/search/users?q=Zhell1');
    });
}

Evenly spaced

You can space out your actions by specifying true as the third (optional) parameter:

const throttledQueueTimeout = require('throttled-queue-timeout');
const throttle = throttledQueueTimeout(10, 1000, true); // at most make 10 requests every second, but evenly spaced.

for (var x = 0; x < 100; x++) {
    throttle(() => {
        // This will fire at most 10 requests a second, spacing them out instead of in a burst.
        return fetch('https://api.github.com/search/users?q=Zhell1');
    });
}

Promises

Starting in version 2.0.0, you can wait for the results of your operation:

const throttledQueueTimeout = require('throttled-queue-timeout');
const throttle = throttledQueueTimeout(10, 1000, true); // at most make 10 requests every second, but evenly spaced.

const usernames = ['Zhell1', 'forward-motion'];
const profiles = await Promise.all(
    usernames.map((username) => throttle(() => {
        return fetch(`https://api.github.com/search/users?q=${username}`);
    }))
);

const justMe = await throttle(() => fetch('https://api.github.com/search/users?q=Zhell1'));

Promises with timeout

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")

const imeout = require('throttled-queue-timeout');
// at most make 4 requests per minute, with timeout after 2 seconds
const throttle = throttledQueueTimeout(4, 60*1000, false, 2000);

for(let i=0; i<5; i++){
    try {
        const res = await throttle(() => fetch('https://api.github.com/search/users?q=Zhell1'));
        console.log("request",i,"success")
    }
    catch(error) {
        console.log("request",i,"error:",error)
    }
}

Typescript support

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:

import throttledQueueTimeout from 'throttled-queue-timeout';
const throttle = throttledQueueTimeout(1, 1000);
const result1 = await throttle<string>(() => '1');
const result2 = await throttle<boolean>(() => Promise.resolve(true));