Giter VIP home page Giter VIP logo

async-throttle's Introduction

async-throttle

Build Status -Coverage Status semantic-release Commitizen friendly

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

Installing

npm install --save @jcoreio/async-throttle

Usage

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 throttled = throttle(foo, 10, {
  getNextArgs: ([a], [b]) => [Math.min(a, b)]
})
throttled(2)
throttled(1)
throttled(3)
// foo will be called with 1

// time passes...

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

async-throttle's People

Contributors

jedwards1211 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.