Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@riteable/q-worker

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@riteable/q-worker

Rate limit and queue async functions.

  • 1.0.7
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

Q-Worker

Rate limit and queue async functions.

Description

This module lets you queue up async tasks to be executed at a predetermined interval. For example, if you're working with a REST API that's rate limited, you can use this module to only execute requests X amount of times per second.

Install

You can install using npm:

$ npm i @riteable/q-worker

Usage

const worker = require('q-worker')

const queue = worker({
    delay: 1000,
    concurrent: 1
})

function someAsyncTask () {
    return new Promise((resolve) => {
        setTimeout(() => resolve(new Date()), 100)
    })
}

const task1 = queue.add(someAsyncTask)
const task2 = queue.add(someAsyncTask)
const task3 = queue.add(someAsyncTask)

Promise.all([task1, task2, task3])
    .then(console.log)
    .catch(console.error)

The console.log would output something like the following:

[
  2020-09-17T12:00:56.471Z,
  2020-09-17T12:00:57.474Z,
  2020-09-17T12:00:58.474Z
]

The output shows the timestamps are all 1 second (1000ms) apart, which is determined by the delay: 1000 setting.

If the concurrent option is set to 2, you might get something like this:

[
  2020-09-17T12:04:15.035Z,
  2020-09-17T12:04:15.035Z,
  2020-09-17T12:04:16.038Z
]

The first two tasks are executed at the same time, but the third task is delayed by the delay amount.

API

Config

  • delay: The amounts of milliseconds between tasks.
  • concurrent: The amount of tasks executed at the same time.

Methods

Available on the configured object, eg.: queue.add().

  • add(fn): Add an async function to the queue.

Events

Available on the configured object, through the events key, eg.: queue.events.on().

  • 'done': Fired when all tasks are done executing.

Keywords

FAQs

Package last updated on 15 Dec 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc