0.0.4 • Published 1 year ago

nyxdefer v0.0.4

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

cover npm version npm downloads bundle JSDocs License

🚀 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:

#nyxi
nyxi nyxdefer

# pnpm
pnpm add nyxdefer

# npm
npm install nyxdefer

# yarn
yarn add nyxdefer

Import:

// ESM
import { nyxdefer } from 'nyxdefer'

// CommonJS
const { nxdefer } = require('nyxdefer')

Debounce function:

const nyxdeferred = debounce(async () => {
   // Some heavy stuff
}, 25)

When calling nyxdefered, 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 nyxdeferred = debounce(async () => {
   // Some heavy stuff
}, 25, { leading: true })

If executing async function takes longer than nyxdefer value, duplicate calls will be still prevented a last call will happen. To disable this behavior, we can set trailing: false option:

const nyxdeferred = nyxdefer(async () => {
   // Some heavy stuff
}, 25, { trailing: false })

💻 Development

  • Clone this repository
  • Navigate to packages/nyxdefer
  • Enable Corepack using corepack enable (use npm i -g corepack for Node.js < 16.10)
  • Install dependencies using nyxi
  • Run interactive tests using nyxr dev

License

Made with 💞

Based on sindresorhus/p-debounce.

Published under MIT License.

0.0.4

1 year ago

0.0.3

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago