# p-debounce

> Debounce promise-returning & async functions

Latest version **5.1.0** (published 2025-10-25) · MIT license · 0 weekly downloads

## Install

```sh
npm install p-debounce
pnpm add p-debounce
yarn add p-debounce
bun add p-debounce
```

## Health

**Score 48/100 (D)** — status: stable.

Positive: has types package; no vulnerabilities.

Warnings: low downloads; no esm support.

## Facts

| | |
|---|---|
| Version | 5.1.0 |
| Published | 2025-10-25 |
| First published | 2016-10-21 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/p-debounce) |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 238 |
| Author | Sindre Sorhus |
| Maintainers | sindresorhus |
| Keywords | promise, debounce, debounced, limit, limited, concurrency, throttle, throat, interval, rate, batch, ratelimit, task, queue, async, await, promises, bluebird |

## Links

- npm: https://www.npmjs.com/package/p-debounce
- Repository: https://github.com/sindresorhus/p-debounce
- Homepage: https://github.com/sindresorhus/p-debounce#readme
- Issues: https://github.com/sindresorhus/p-debounce/issues
- Funding: https://github.com/sponsors/sindresorhus
- npm.io page: https://npm.io/package/p-debounce

## Alternatives

- [@commercetools/sync-actions](https://npm.io/package/@commercetools/sync-actions.md) — 25.1K weekly downloads
- [cwait](https://npm.io/package/cwait.md) — 21.4K weekly downloads
- [@ledgerhq/hw-app-cosmos](https://npm.io/package/@ledgerhq/hw-app-cosmos.md) — 4.2K weekly downloads
- [@financial-times/o-loading](https://npm.io/package/@financial-times/o-loading.md) — 2.8K weekly downloads
- [fa](https://npm.io/package/fa.md) — 185 weekly downloads

## Recent versions

- 5.1.0 (latest) — 2025-10-25
- 5.0.0 — 2025-09-16
- 4.0.0 — 2021-04-20
- 3.0.2 — 2021-03-29
- 3.0.1 — 2021-01-22
- 3.0.0 — 2021-01-18
- 2.1.0 — 2019-04-05
- 2.0.0 — 2019-03-12
- 1.0.0 — 2016-10-21

## README

# p-debounce

> [Debounce](https://css-tricks.com/debouncing-throttling-explained-examples/) promise-returning & async functions

## Install

```sh
npm install p-debounce
```

## Usage

```js
import pDebounce from 'p-debounce';

const expensiveCall = async input => input;

const debouncedFunction = pDebounce(expensiveCall, 200);

for (const number of [1, 2, 3]) {
	(async () => {
		console.log(await debouncedFunction(number));
	})();
}
//=> 3
//=> 3
//=> 3
```

## API

### pDebounce(fn, wait, options?)

Returns a function that delays calling `fn` until after `wait` milliseconds have elapsed since the last time it was called.

#### fn

Type: `Function`

Promise-returning/async function to debounce.

#### wait

Type: `number`

Milliseconds to wait before calling `fn`.

#### options

Type: `object`

##### before

Type: `boolean`\
Default: `false`

Call the `fn` on the [leading edge of the timeout](https://css-tricks.com/debouncing-throttling-explained-examples/#article-header-id-1). Meaning immediately, instead of waiting for `wait` milliseconds.

##### signal

Type: `AbortSignal`

An [`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) to cancel the debounced function.

### pDebounce.promise(function_, options?)

Execute `function_` unless a previous call is still pending, in which case, return the pending promise. Useful, for example, to avoid processing extra button clicks if the previous one is not complete.

```js
import {setTimeout as delay} from 'timers/promises';
import pDebounce from 'p-debounce';

const expensiveCall = async value => {
	await delay(200);
	return value;
};

const debouncedFunction = pDebounce.promise(expensiveCall);

for (const number of [1, 2, 3]) {
	(async () => {
		console.log(await debouncedFunction(number));
	})();
}
//=> 1
//=> 1
//=> 1
```

#### function_

Type: `Function`

Promise-returning/async function to debounce.

#### options

Type: `object`

##### after

Type: `boolean`\
Default: `false`

If a call is made while a previous call is still running, queue the latest arguments and run the function again after the current execution completes.

Use cases:
- With `after: false` (default): API fetches, data loading, read operations - concurrent calls share the same result.
- With `after: true`: Saving data, file writes, state updates - ensures latest data is never lost.

```js
import {setTimeout as delay} from 'timers/promises';
import pDebounce from 'p-debounce';

const save = async data => {
	await delay(200);
	console.log(`Saved: ${data}`);
	return data;
};

const debouncedSave = pDebounce.promise(save, {after: true});

// If data changes while saving, it will save again with the latest data
debouncedSave('data1');
debouncedSave('data2'); // This will run after the first save completes
//=> Saved: data1
//=> Saved: data2
```

## Related

- [p-throttle](https://github.com/sindresorhus/p-throttle) - Throttle promise-returning & async functions
- [p-limit](https://github.com/sindresorhus/p-limit) - Run multiple promise-returning & async functions with limited concurrency
- [p-memoize](https://github.com/sindresorhus/p-memoize) - Memoize promise-returning & async functions
- [debounce-fn](https://github.com/sindresorhus/debounce-fn) - Debounce a function
- [More…](https://github.com/sindresorhus/promise-fun)

---
_Source: https://npm.io/package/p-debounce · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
