New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

throttle-promise

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

throttle-promise

Throttle promise-returning & async functions

  • 1.0.4
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
461
decreased by-17.83%
Maintainers
1
Weekly downloads
 
Created
Source

throttle-promise

Throttle promise-returning & async functions

It also works with normal functions.

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

NOTE: This is a fork of p-throttle, but without ES6 in the source code.

Install

$ npm install throttle-promise

Usage

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

const throttle = require('throttle-promise');

const now = Date.now();

const throttled = throttle(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

throttle(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 in milliseconds.

throttledFn.abort()

Abort pending executions. All unresolved promises are rejected with a throttle.AbortError error.

  • 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

Keywords

FAQs

Package last updated on 08 Nov 2017

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