GithubHelp home page GithubHelp logo

p-throttle's Introduction

p-throttle Build Status

Throttle promise-returning & async functions

It also works with normal functions.

Useful for rate limiting calls to an external API, for example.

Install

$ npm install --save p-throttle

Usage

Here, the trottled function is only called twice a second:

const pThrottle = require('p-throttle');

const now = Date.now();

const throttled = pThrottle(i => {
	const secDiff = ((Date.now() - now) / 1000).toFixed();
	return Promise.resolve(`${i}: ${secDiff}s`);
}, 2, 1000);

for (let i = 1; i <= 6; i++) {
	throttled(i).then(console.log);
}
//=> 1: 0s
//=> 2: 0s
//=> 3: 1s
//=> 4: 1s
//=> 5: 2s
//=> 6: 2s

API

pThrottle(fn, limit, interval)

Returns a throttled version of fn.

fn

Type: Function

Promise-returning/async function or a normal function.

limit

Type: number

Maximum number of calls within an interval.

interval

Type: number

Timespan for limit.

Related

  • p-debounce - Debounce promise-returning & async functions
  • p-limit - Run multiple promise-returning & async functions with limited concurrency
  • p-memoize - Memoize promise-returning & async functions
  • More…

License

MIT © Sindre Sorhus

p-throttle's People

Contributors

sindresorhus avatar

Watchers

Dylan Barrell 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.