Socket
Book a DemoInstallSign in
Socket

throttle-wait

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

throttle-wait

Run a function at max once per x milliseconds. Subsequent calls will wait.

0.0.5
latest
npmnpm
Version published
Weekly downloads
7
Maintainers
1
Weekly downloads
 
Created
Source

throttle-wait

Run a function at max once per x milliseconds. Subsequent calls will wait.

  • 0 dependencies
  • Typescript friendly (can use @Decorators)

Installation

$ npm install throttle-wait

or

$ yarn add throttle-wait

Simple usage

import { throttle } from 'throttle-wait'

function myFn() {
  console.log(new Date())
}

const myFnThrottled = throttle(5 * 1000, myFn) // 5s

// 2020-01-01T00:00:00.114Z
await myFnThrottled()

// 2020-01-01T00:00:05.120Z
await myFnThrottled()

// 2020-01-01T00:00:10.125Z
await myFnThrottled()

Typescript decorator usage

import { Throttle } from 'throttle-wait'

class MyClass {
  @Throttle(5 * 1000) // 5s
  public async myFn() {
    console.log(new Date())
  }
}
const myClass = new MyClass()

// 2020-01-01T00:00:00.114Z
await myClass.myFn()

// 2020-01-01T00:00:05.120Z
await myClass.myFn()

// 2020-01-01T00:00:10.125Z
await myClass.myFn()

IMPORTANT: Make your function async. The decorator will return an async function. This will ensure that the intellisense of your IDE tells you that your function is async.

Options

const myFnThrottled = throttle(throttleTime, myFn, {
  max: 100,
  onThrottle: (next, queue) => {
    console.log(`Throttling, will run in ${next}ms (${queue} in the queue)`)
  },
})
  • max (default=100): The maximum number of calls in the queue before throwing an error
  • onThrottle: Callback when your function is being throttled

Errors

Throttle backpressure error: Throttle is being called faster than it can run

This means that your function is being called a lot asynchronously. An error is thrown when there are 100 calls in the queue (configurable).

Advance throttling

If you want to ignore subsequent calls, look at lodash throttle function

  • https://www.npmjs.com/package/lodash.throttle

If you want to have more throttling control, you can check out these great libs.

FAQs

Package last updated on 21 Aug 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

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.