# awesome-debounce-promise

> Debounce your async calls

Latest version **2.1.0** (published 2019-02-26) · MIT license · 0 weekly downloads

## Install

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

## Health

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

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.1.0 |
| Published | 2019-02-26 |
| First published | 2018-09-13 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=8 |
| Dependencies | 4 |
| Unpacked size | 45.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 392 |
| Author | slorber |
| Maintainers | slorber |

## Links

- npm: https://www.npmjs.com/package/awesome-debounce-promise
- Repository: https://github.com/slorber/awesome-debounce-promise
- npm.io page: https://npm.io/package/awesome-debounce-promise

## Dependencies (4)

- [debounce-promise](https://npm.io/package/debounce-promise.md) ^3.1.0
- [@types/debounce-promise](https://npm.io/package/@types/debounce-promise.md) ^3.1.1
- [awesome-imperative-promise](https://npm.io/package/awesome-imperative-promise.md) ^1.0.1
- [awesome-only-resolves-last-promise](https://npm.io/package/awesome-only-resolves-last-promise.md) ^1.0.3

## Recent versions

- 2.1.0 (latest) — 2019-02-26
- 2.0.1 — 2019-02-23
- 2.0.0 — 2019-02-22
- 1.0.0 — 2018-09-13
- 0.0.2 — 2018-09-13
- 0.0.1 — 2018-09-13

## README

# Awesome Debounce Promise

[![NPM](https://img.shields.io/npm/dm/awesome-debounce-promise.svg)](https://www.npmjs.com/package/awesome-debounce-promise)
[![Build Status](https://travis-ci.com/slorber/awesome-debounce-promise.svg?branch=master)](https://travis-ci.com/slorber/awesome-debounce-promise)

Debounce your async calls with **React** in mind.

Forget about:

- concurrency issues when promise resolves in "unexpected" order
- leaving promise land for callback hell of Lodash / Underscore

From the author of [this famous SO question](https://stackoverflow.com/a/28046731/82609) about debouncing with React.

# Install

`yarn add awesome-debounce-promise`

`npm install awesome-debounce-promise --save`

```jsx harmony
import AwesomeDebouncePromise from 'awesome-debounce-promise';

const asyncFunction = () => fetch('/api');

const asyncFunctionDebounced = AwesomeDebouncePromise(
  asyncFunction,
  500,
  options,
);
```

# Usecases

## Debouncing a search input

```jsx harmony
const searchAPI = text => fetch('/search?text=' + encodeURIComponent(text));

const searchAPIDebounced = AwesomeDebouncePromise(searchAPI, 500);

class SearchInputAndResults extends React.Component {
  state = {
    text: '',
    results: null,
  };

  handleTextChange = async text => {
    this.setState({ text, results: null });
    const result = await searchAPIDebounced(text);
    this.setState({ result });
  };

  componentWillUnmount() {
    this.setState = () => {};
  }
}
```

When calling `debouncedSearchAPI`:

- it will debounce the api calls. The API will only be called when user stops typing
- each call will return a promise
- only the promise returned by the last call will resolve, which will prevent the concurrency issues
- there will be at most a single `this.setState({ result });` call per api call

## Debouncing the background saving of some form inputs

```jsx harmony
const saveFieldValue = (fieldId, fieldValue) =>
  fetch('/saveField', {
    method: 'PUT',
    body: JSON.stringify({ fieldId, fieldValue }),
  });

const saveFieldValueDebounced = AwesomeDebouncePromise(
  saveFieldValue,
  500,
  // Use a key to create distinct debouncing functions per field
  { key: (fieldId, text) => fieldId },
);

class SearchInputAndResults extends React.Component {
  state = {
    value1: '',
    value2: '',
  };

  onFieldTextChange = async (fieldId, fieldValue) => {
    this.setState({ [fieldId]: fieldValue });
    await saveFieldValueDebounced(fieldId, fieldValue);
  };

  render() {
    const { value1, value2 } = this.state;
    return (
      <form>
        <input
          value={value1}
          onChange={e => onFieldTextChange(1, e.target.value)}
        />
        <input
          value={value2}
          onChange={e => onFieldTextChange(2, e.target.value)}
        />
      </form>
    );
  }
}
```

Thanks to the `key` feature, the 2 fields will be debounced independently from each others. In practice, one debounced function is created for each key.

# Options

```jsx harmony
const DefaultOptions = {
  // One distinct debounced function is created per key and added to an internal cache
  // By default, the key is null, which means that all the calls
  // will share the same debounced function
  key: (...args) => null,

  // By default, a debounced function will only resolve
  // the last promise it returned
  // Former calls will stay unresolved, so that you don't have
  // to handle concurrency issues in your code
  // Setting this to false means all returned promises will resolve to the last result
  onlyResolvesLast: true,
};
```

Other debouncing options are available and provided by an external low-level library: [debounce-promise](https://github.com/bjoerge/debounce-promise)

# FAQ

### How can I cancel the debouncing?

You can easily add promise cancellation support to this lib with [awesome-imperative-promise](https://github.com/slorber/awesome-imperative-promise), lib that is already used internally.

### Why is my debouncing function always firing and is not debounced?

The debouncing function returned by the lib is stateful. If you want deboucing to work fine, make sure to avoid recreating this function everytime. This is the same behavior as regular callback-based debouncing functions.

Instead of this:

```js
handleTextChange = async text => {
  const searchAPI = text => fetch('/search?text=' + encodeURIComponent(text));
  const searchAPIDebounced = AwesomeDebouncePromise(searchAPI, 500);
  this.setState({ text, results: null });
  const result = await searchAPIDebounced(text);
  this.setState({ result });
};
```

Do this:

```js
const searchAPI = text => fetch('/search?text=' + encodeURIComponent(text));
const searchAPIDebounced = AwesomeDebouncePromise(searchAPI, 500);

handleTextChange = async text => {
  this.setState({ text, results: null });
  const result = await searchAPIDebounced(text);
  this.setState({ result });
};
```

# Hire a freelance expert

Looking for a React/ReactNative freelance expert with more than 5 years production experience?
Contact me from my [website](https://sebastienlorber.com/) or with [Twitter](https://twitter.com/sebastienlorber).

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