You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
8 months ago | |
|---|---|---|
| .. | ||
| dist | 8 months ago | |
| LICENSE | 8 months ago | |
| README.md | 8 months ago | |
| package.json | 8 months ago | |
README.md
perfect-debounce
An improved debounce function with Promise support.
- Well tested debounce implementation
- Native Promise support
- Avoid duplicate calls while promise is being resolved
- Configurable
trailingandleadingbehavior
Usage
Install package:
# npm
npm install perfect-debounce
# yarn
yarn add perfect-debounce
# pnpm
pnpm add perfect-debounce
Import:
// ESM
import { debounce } from 'perfect-debounce'
// CommonJS
const { debounce } = require('perfect-debounce')
Debounce function:
const debounced = debounce(async () => {
// Some heavy stuff
}, 25)
When calling debounced, it will wait at least for 25ms as configured before actually calling our function. This helps to avoid multiple calls.
To avoid initial wait, we can set leading: true option. It will cause function to be immediately called if there is no other call:
const debounced = debounce(async () => {
// Some heavy stuff
}, 25, { leading: true })
If executing async function takes longer than debounce value, duplicate calls will be still prevented a last call will happen. To disable this behavior, we can set trailing: false option:
const debounced = debounce(async () => {
// Some heavy stuff
}, 25, { trailing: false })
💻 Development
- Clone this repository
- Enable Corepack using
corepack enable(usenpm i -g corepackfor Node.js < 16.10) - Install dependencies using
pnpm install - Run interactive tests using
pnpm dev
License
Made with 💛
Based on sindresorhus/p-debounce.
Published under MIT License.