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

@jcoreio/async-throttle

Package Overview
Dependencies
Maintainers
13
Versions
20
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@jcoreio/async-throttle

throttle async and promise-returning functions. Other packages don't do it right.

  • 1.3.0
  • Source
  • npm
  • Socket score

Version published
Maintainers
13
Created
Source

async-throttle

CircleCI Coverage Status semantic-release Commitizen friendly npm version

throttle async and promise-returning functions. Other packages don't do it right.

Installing

npm install --save @jcoreio/async-throttle

Usage

const throttle = require('@jcoreio/async-throttle')
function throttle<Args: Array<any>, Value>(
  func: (...args: Args) => Promise<Value>,
  wait: ?number,
  options?: {
    getNextArgs?: (current: Args, next: Args) => Args
  }
): (...args: Args) => Promise<Value>;

Creates a throttled function that only invokes func at most once per every wait milliseconds, and also waits for the Promise returned by the previous invocation to finish (it won't invoke func in parallel).

The promise returned by the throttled function will track the promise returned by the next invocation of func.

If wait is falsy, it is treated as 0, which causes func to be invoked on the next tick afte the previous invocation finishes.

By default, func is called with the most recent arguments to the throttled function. You can change this with the getNextArgs option -- for example, if you want to invoke func with the minimum of all arguments since the last invocation:

const throttledFn = throttle(foo, 10, {
  getNextArgs: ([a], [b]) => [Math.min(a, b)],
})
throttledFn(2)
throttledFn(1)
throttledFn(3)
// foo will be called with 1

// time passes...

throttledFn(4)
throttledFn(6)
throttledFn(5)
// foo will be called with 4

throttledFn.cancel()

Cancels the pending invocation, if any. All Promises tracking the pending invocation will be rejected with a CancelationError (const {CancelationError} = require('@jcoreio/async-throttle')). However, if an invocation is currently running, all Promises tracking the current invocation will be fulfilled as usual.

Returns a Promise that will resolve once the current invocation (if any) is finished.

throttledFn.flush()

Cancels the wait before the pending invocation, if any. The pending invocation will still wait for the current invocation (if any) to finish, but will begin immediately afterward, even if wait has not elapsed.

Returns a Promise that will resolve once the current invocation (if any) is finished.

Keywords

FAQs

Package last updated on 01 Oct 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