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

just-queue

v1.0.1

Published

A Simple-To-Use Promise Based Queue For Concurrency & Throttle Limiting.

Downloads

12

Readme

JustQueue: A Simple-To-Use Promise Based Queue For Concurrency & Throttle Limiting.

NPM version NPM downloads Language grade: JavaScript GitHub issues GitHub stars GitHub license

Motivation

JustQueue aims to simplify the process of setting up local queues in which you may need to throttle or limit the concurrency of asynchronous operations. The most common use case would using JustQueue in front an outgoing third party API request that may have its own rate and concurrency limits. In this scenario, JustQueue can allow asynchronous calls to this API to be appropriately throttled without going over the specified limits.

Some of the prominent features implemented are:

  • Promise Based Queue
  • Asynchronous By Nature
  • CPU & Memory Efficient
  • Various Limiting Options

Installation

JustQueue can be installed using node package manager (npm)

npm i just-queue

Table Of Contents

Examples

Below are various examples that make use of JustQueue.

Example: Concurrenly Limiting Queue To Third-Party API

const JustQueue = new JustQueue({
    max_concurrent: 4
});

async function get_currency_data(){
    // Assume this function makes a POST request to a third-party API
    // that only allows 4 conncurrent requests to be made with your API key
}

async function throttled_get(){
    return JustQueue.queue(() => get_currency_data());
}

// We can now call this function more than 4 times but JustQueue will
// automatically ensure that no more than 4 maximum concurrent requests are made at any given time
throttled_get()
.then((data) => console.log('Got Currency Data!', data))
.catch((error) => console.log('Failed To Get Currency Data: ', error));
});

Example: Throttle Limiting Queue To Third-Party API

const JustQueue = new JustQueue({
    throttle: {
        rate: 4,
        interval: 5000
    }
});

async function get_currency_data(){
    // Assume this function makes a POST request to a third-party API
    // that only allows 4 requests every 5 seconds with your API key.
}

async function throttled_get(){
    return JustQueue.queue(() => get_currency_data());
}

// We can now call this function more than 4 times but JustQueue will
// automatically ensure that no more than 4 requests are made every 5 seconds.
throttled_get()
.then((data) => console.log('Got Currency Data!', data))
.catch((error) => console.log('Failed To Get Currency Data: ', error));
});

JustQueue

Below is a breakdown of the JustQueue object class generated while creating a new JustQueue instance.

JustQueue Constructor Options

  • max_concurrent [Number]: Maximum number of operations to execute concurrently.
    • Default: Infinity
  • max_queued [Number]: Maximum number of operations to have queued at any given time.
    • Default: Infinity
    • Note: The operation will reject with an Error that has the message QUEUE_FULL.
  • timeout [Number]: Maximum amount of time in milliseconds after which a queued operation is aborted.
    • Default: Infinity
    • Note: The operation will reject with an Error that has the message TIMED_OUT.
  • throttle [Object]: Throttle limiter options.
    • rate [Number]: Number of operations to execute in a throttle interval.
      • Default: Infinity
    • interval [Number]: Interval time in milliseconds to throttle operations.
      • Default: Infinity

JustQueue Instance Properties

| Property | Type | Description | | :-------- | :------- | :------------------------- | | active | Number | Number of concurrently active operations. | | queued | Number | Number of queued operations. |

JustQueue Instance Methods

  • queue(Function: operation): Queues an operation
    • Returns a Promise
    • Note operation must be async or return a Promise.
    • Asynchronous Example: queue(async () => { /* Your Code Here */});
    • Promise Example: queue(() => new Promise((resolve, reject) => { /* Your Code Here */});

License

MIT