# debounce-promise

> Create a debounced version of a promise returning function

Latest version **3.1.2** (published 2019-05-05) · MIT license · 0 weekly downloads

## Install

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

## Health

**Score 23/100 (F)** — status: abandoned.

Positive: has types package; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.1.2 |
| Published | 2019-05-05 |
| First published | 2015-10-18 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/debounce-promise) |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 18.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 240 |
| Author | Bjørge Næss |
| Maintainers | bjoerge |
| Keywords | promise, batch, accumulate, debounce, throttle, ratelimit |

## Links

- npm: https://www.npmjs.com/package/debounce-promise
- Repository: https://github.com/bjoerge/debounce-promise
- Issues: https://github.com/bjoerge/debounce-promise/issues
- npm.io page: https://npm.io/package/debounce-promise

## 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

- 3.1.2 (latest) — 2019-05-05
- 3.1.1 — 2019-05-05
- 3.1.0 — 2018-02-09
- 3.0.2 — 2017-11-08
- 3.0.1 — 2017-01-28
- 3.0.0 — 2017-01-28
- 2.1.1 — 2016-10-31
- 2.1.0 — 2016-10-16
- 2.0.1 — 2016-08-24
- 2.0.0 — 2016-08-17
- 1.1.1 — 2016-08-17
- 1.1.0 — 2016-04-01
- 1.0.5 — 2015-12-21
- 1.0.4 — 2015-10-19
- 1.0.3 — 2015-10-19
- … 3 more at https://npm.io/package/debounce-promise/versions

## README

# debounce-promise

[![Build Status](https://travis-ci.org/bjoerge/debounce-promise.svg)](https://travis-ci.org/bjoerge/debounce-promise)
[![Standard - JavaScript Style Guide](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com/)

[![NPM](https://nodei.co/npm/debounce-promise.png)](https://nodei.co/npm/debounce-promise/)

Create a debounced version of a promise returning function

## Install

    npm i -S debounce-promise


## Usage example

```js

var debounce = require('debounce-promise')

function expensiveOperation(value) {
  return Promise.resolve(value)
}

var saveCycles = debounce(expensiveOperation, 100);

[1, 2, 3, 4].forEach(num => {
  return saveCycles('call no #' + num).then(value => {
    console.log(value)
  })
})

// Will only call expensiveOperation once with argument `4` and print:
//=> call no #4
//=> call no #4
//=> call no #4
//=> call no #4
```

### With leading=true

```js
var debounce = require('debounce-promise')

function expensiveOperation(value) {
  return Promise.resolve(value)
}

var saveCycles = debounce(expensiveOperation, 100, {leading: true});

[1, 2, 3, 4].forEach(num => {
  return saveCycles('call no #' + num).then(value => {
    console.log(value)
  })
})

//=> call no #1
//=> call no #4
//=> call no #4
//=> call no #4
```

### With accumulate=true

```js
var debounce = require('debounce-promise')

function squareValues (argTuples) {
  return Promise.all(argTuples.map(args => args[0] * args[0]))
}

var square = debounce(squareValues, 100, {accumulate: true});

[1, 2, 3, 4].forEach(num => {
  return square(num).then(value => {
    console.log(value)
  })
})

//=> 1
//=> 4
//=> 9
//=> 16
```

## Api
`debounce(func, [wait=0], [{leading: true|false, accumulate: true|false})`

Returns a debounced version of `func` that delays invoking until after `wait` milliseconds.

Set `leading: true` if you
want to call `func` and return its promise immediately.

Set `accumulate: true` if you want the debounced function to be called with an array of all the arguments received while waiting.

Supports passing a function as the `wait` parameter, which provides a way to lazily or dynamically define a wait timeout.


## Example timeline illustration

```js
function refresh() {
  return fetch('/my/api/something')
}
const debounced = debounce(refresh, 100)
```

```
time (ms) ->   0 ---  10  ---  50  ---  100 ---
-----------------------------------------------
debounced()    | --- P(1) --- P(1) --- P(1) ---
refresh()      | --------------------- P(1) ---
```

```js
const debounced = debounce(refresh, 100, {leading: true})
```
```
time (ms) ->   0 ---  10  ---  50  ---  100 ---  110 ---
--------------------------------------------------------
debounced()    | --- P(1) --- P(2) --- P(2) --- P(2) ---
refresh()      | --- P(1) --------------------- P(2) ---
```

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