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

@jcoreio/async-throttle

v1.6.1

Published

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

Downloads

29,735

Readme

async-throttle

CircleCI codecov semantic-release Commitizen friendly npm version

Throttle async and promise returning functions. Unlike similarly named packages, this behaves much like an async version of lodash.throttle:

  • Only one invocation can be running at a time (similarly named packages don't do this)
  • Has .cancel() and .flush()

Differences from lodash.throttle

  • No leading and trailing options
  • getNextArgs option allows you to customize how the arguments for the next invocation are determined

Installing

npm install --save @jcoreio/async-throttle

Usage

const throttle = require('@jcoreio/async-throttle')
import throttle from '@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.invokeIgnoreResult(args)

Calls the throttled function soon, but doesn't return a promise, catches any CanceledError, and doesn't create any new promises if a call is already pending.

To use this, you should handle all errors inside the throttled function:

const throttled = throttle(async (arg) => {
  try {
    await doSomething(arg)
  } catch (err) {
    // handle error
  }
})

Then call invokeIgnoreResult instead of the throttled function:

throttled.invokeIgnoreResult(arg)

The invokeIgnoreResult method is useful because the following code example would leave 1000 pending promises on the heap, even though the catch block is a no-op:

for (let i = 0; i < 1000; i++) {
  throttled(arg).catch(() => {})
}

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.