Socket
Socket
Sign inDemoInstall

perfect-debounce

Package Overview
Dependencies
0
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    perfect-debounce

[![npm version][npm-version-src]][npm-version-href] [![npm downloads][npm-downloads-src]][npm-downloads-href] [![Github Actions][github-actions-src]][github-actions-href] [![Codecov][codecov-src]][codecov-href]


Version published
Weekly downloads
765K
increased by3.96%
Maintainers
1
Install size
8.87 kB
Created
Weekly downloads
 

Changelog

Source

0.0.1 (2022-03-15)

Features

  • wait for promise (#1) (c0c0865)

Readme

Source

perfect-debounce

npm version npm downloads Github Actions Codecov

An improved debounce function with Promise support.

  • Well tested debounce implementation
  • Native Promise support
  • Avoid duplicate calls while promise is being resolved
  • Configurable trailing and leading behavior

Usage

Install package:

# npm
npm install perfect-debounce

# yarn
yarn install perfect-debounce

# pnpm
pnpm install 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 (use npm i -g corepack for Node.js < 16.10)
  • Install dependencies using yarn install
  • Run interactive tests using yarn dev

License

Made with 💛

Based on sindresorhus/p-debounce.

Published under MIT License.

FAQs

Last updated on 15 Mar 2022

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc