throttle-interval
v1.0.0
Published
Ensures that a function won't be called more times than specified within a time interval. Supports Promise and AbortSignal.
Maintainers
Readme
Throttle by Interval
Ensures that a function won't be called more times than specified within a time interval. Supports Promise and AbortSignal.
- ECMAScript only. Works in any environment.
- Tiny - 500 B minified, 260 B brotlied.
- ESM, CJS and UMD bundles provided.
- No dependencies.
Synopsis
Load three files from a service, which allows only one connection per second:
import throttle from 'throttle-interval'
const load = id => fetch(`http://data.com/${id}`).then(r => r.text())
const schedule = throttle(1000)
const [first, second, third] = await Promise.all([
schedule(() => load(1)),
schedule(() => load(2)),
schedule(() => load(3))
])Installation
This module can be installed locally by NPM or other package manager. If you use Node.js, make sure, that you use version 23 or newer:
npm i throttle-intervalWhen used in the browser, you can load the script from the CDN, ESM, UMD or global:
<script>
import throttle from 'https://unpkg.com/[email protected]/lib/index.js'
</script><script src="https://unpkg.com/[email protected]/require.js"></script>
<script>
require(
['https://unpkg.com/[email protected]/lib/index.umd.min.js'],
function (throttle) {
}
)
</script><script src="https://unpkg.com/[email protected]/lib/index.umd.min.js"></script>
<script>
const throttle = window.throttleInterval
</script>Details
The worker function can be synchronous or asynchronous.
The worker function won't be executed synchronously right away, when scheduled.
Its execution will be performed in the next event loop cycle by a zero setTimeout.
It's safe to add callbacks to the promise returned from the scheduler function
before the worker function gets executed.
If execution of a worker function takes longer than the throttling interval, the next worker function will be executed right after the previous one has finished. The throttling interval is a minimum time interval between two function executions.
If signal is passed to the scheduler function, it'll be passed to the worker
function too. If the worker function performs multiple asynchronous calls, it
should check the state of the signal and abort early, if it gets aborted.
API
/**
* A worker function.
*
* @param options Optional settings for the worker, including an AbortSignal
* to cancel the execution of the worker function.
* @returns The result of the worker function, or a promise that resolves to it.
*/
export type Worker<T = any> = (options?: { signal?: AbortSignal }) => T | Promise<T>
/**
* Schedules a worker function.
*
* @param worker The function to be executed in a controlled manner.
* @param options Optional settings for the scheduler, including an AbortSignal to cancel the execution.
* @returns A promise that will be settled with the result of the worker function
* when the worker function has been executed.
*/
export type Scheduler<T = any> = (worker: Worker<T>, options?: { signal?: AbortSignal }) => Promise<T>
/**
* Creates a scheduler function.
*
* @param interval The minimum time interval (in milliseconds) between the calls
* of the throttled worker functions.
* @returns A scheduler function that will call worker functions no more frequently
* than the specified interval.
*/
export default function throttle<T = any>(interval: number): Scheduler<T>Examples
Passing an AbortSignal to the scheduler function will abort the worker function
as early as possible without even executing it, if it hasn't been executed yet.
The worker function may access the AbortSignal, in the object passed to it
as the first argument.
import throttle from 'throttle-interval'
const load = (id, signal) => fetch(`http://data.com/${id}`, { signal }).then(r => r.text())
const schedule = throttle(1000)
const controller = new AbortController()
const { signal } = controller
const [first, second, third] = await Promise.all([
schedule(({ signal }) => load(1, signal), { signal }),
schedule(({ signal }) => load(2, signal), { signal }),
schedule(({ signal }) => load(3, signal), { signal })
])
controller.abort()import throttle from 'throttle-interval'
const load = (id, signal) => fetch(`http://data.com/${id}`, { signal }).then(r => r.text())
const schedule = throttle(1000)
const controller = new AbortController()
const { signal } = controller
const [first, second, third] = await Promise.all([
schedule(({ signal }) => load(1, signal), { signal }),
schedule(({ signal }) => load(2, signal), { signal }),
schedule(({ signal }) => load(3, signal), { signal })
])
controller.abort()Contributing
In lieu of a formal styleguide, take care to maintain the existing coding style. Lint and test your code.
License
Copyright (c) 2026 Ferdinand Prantl
Licensed under the MIT license.
