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

throttle-async

v0.0.2

Published

A throttled function that delays invoking asynchronous functions.

Downloads

88

Readme

throttle-async

Build Status NPM Package Dependency status devDependency status

NPM

A throttled asynchronous function that invokes at most once per every given time window, excluding the leading invocation.

Preliminaries

A throttled function groups sequential calls to a function within a period. Only the first and last call in that group is executed. The others are simply ignored with rejections as if no calls to them ever happened.

Say d is a throttled function of f, var d = throttle(f, 1000);, where f is an asynchronous function that returns a promise to be resolved in 400ms since it gets called. Below is a depiction of such a sequence of calls to d.

seconds elapsed    0         1         2         3
d called           - d d d - - - - d - - d - d d - - -
                     | x |         |     |   x |
f called             f   +---f     f     f     +-f
                     |       |     |     |       |
promise resolved     +-> *   +-> * +-> * +-> *   +-> *

d denotes a call to function d, f denotes a call to function f, and * denotes a promise returned by f is resolved. x denotes a call to d returns a rejected promise.

For promise-based asynchronous functions, this package ignores function calls by rejecting the promises with a customizable object for the sake of telling which/when an ignorance happens.

Installation

npm install throttle-async --save

Usage

var throttle = require( 'throttle-async' );

/**
  * throttle(func, [wait=0], [options={}])
  *
  * @param {Function} func The function to throttle.
  * @param {number} [wait=0] The number of milliseconds to delay.
  * @param {Object} [options={}] The options object.
  * @param {boolean} [options.leading=true] Specify invoking on the leading edge of the timeout.
  * @param {cancelObj} [options.cancelObj='canceled'] Specify the error object to be rejected.
  * @returns {Function} Returns the new throttled function.
  */

Example

Promise

var throttle = require( 'throttle-async' );

var f = value => new Promise( resolve => setTimeout( () => resolve( value ), 400 ) );
var throttled = throttle( f, 1000 );

var promises = [ 'foo', 'bar', 'baz' ].map( throttled );

promises.forEach( promise => {
  promise
    .then( res => console.log( 'resolved:', res ) )
    .catch( err => console.log( 'rejected:', err ) )
});

// Output:
// resolved: foo
// rejected: canceled
// resolved: baz

In the example above, f is an asynchronous function which returns a promise. The promise is resolved with the input after 400ms. throttled is a throttled function of f with a delay of 1s.

The throttled function is called consecutively by the callback of Array.proptotype.map, with 'foo' 'bar', and 'baz' being the input value respectively. The returned promises are next fullfilled by printing the resolved result or rejected error on the console.

This snippet results in the given output. The first promise was resolved since it is the leading call. The second was rejected since the third came soon. The third promise was resolved since it is the last call in the specified time window of 1 second.

async/await

Same thing when it comes to asynchronous ES7 async/await functions. Take the prior example and transform the f into an ES7 async function.

var f = async value => await new Promise( resolve => setTimeout( () => resolve( value ), 400 ) );

Same output can be expected.

Test

npm test

License

MIT. See LICENSE.md for details.