A minimal, vanilla JS library to throttle/rate-limit fetch, API calls and other promise returning functions ♻️
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src
test
.babelrc
.editorconfig
.eslintignore
.eslintrc
.gitignore
.npmignore
.travis.yml
LICENSE
README.md
package.json
yarn.lock

README.md

promise-me-later

A minimal, vanilla JS library to throttle/rate-limit fetch, API calls and other promise returning functions ♻️

Version Build Downloads

When to use

When you need to limit any promise returning function call rate but still want each call to happen over time. For example, a good use case for this is to limit calling APIs from fetch in browser or node-fetch in node environment.

Usage

As a node module

npm i promise-me-later
import promiseMeLater from 'promise-me-later';

// any promise returning function
const delay = () => new Promise((resolve, reject) => {
	console.log('Resolving promise');
	setTimeout(() => {
		resolve();
		console.log('Promise resolved');
	}, 1000);	// resolves the promise after 1 second
})

// returns a rate limited function
const limit = promiseMeLater(delay, 1, 2000);	// call at max once in 2 seconds

// also try this with fetch call, since it also returns a promise
const limitFetch = promiseMeLater(fetch, 2, 1000);
/* will only be called at max twice in 1 second
 * as soon as a promise is resloved, another will be picked
 * from the queue and invoked
 *  Now you can use `limitFetch` instead of `fetch`
 * and all your `fetch` calls will be rate limited */

As a UMD

Get the latest UMD from:

https://unpkg.com/promise-me-later/umd/index.js

Motivation

I was looking for a simple way to rate limit fetch calls, the caveat being that each call should happen (unlike throttling).

For best results, listen to Promise by Slash 🎸 and Chris ❤️