{"name":"perfect-debounce","version":"2.1.0","description":"<!-- automd:badges color=yellow codecov bundlephobia packagephobia  -->","repository":{"type":"git","url":"git+https://github.com/unjs/perfect-debounce.git"},"license":"MIT","sideEffects":false,"type":"module","exports":{".":"./dist/index.mjs"},"main":"./dist/index.mjs","module":"./dist/index.mjs","types":"./dist/index.d.mts","scripts":{"build":"obuild","dev":"vitest dev","lint":"eslint . && prettier --check src test","lint:fix":"eslint . --fix && prettier -w src test","release":"pnpm test && pnpm build && changelogen --release --push && npm publish","test":"vitest run --coverage"},"devDependencies":{"@types/node":"^25.0.10","@vitest/coverage-v8":"^4.0.17","automd":"^0.4.2","changelogen":"^0.6.2","eslint":"^9.39.2","eslint-config-unjs":"^0.6.2","in-range":"^3.0.0","obuild":"^0.4.18","prettier":"^3.8.1","time-span":"^5.1.0","typescript":"^5.9.3","vitest":"^4.0.17"},"packageManager":"pnpm@10.28.1","gitHead":"d3f83001dad6faa2090bd1aadab7312843fe6b79","_id":"perfect-debounce@2.1.0","bugs":{"url":"https://github.com/unjs/perfect-debounce/issues"},"homepage":"https://github.com/unjs/perfect-debounce#readme","_nodeVersion":"24.11.1","_npmVersion":"11.6.2","dist":{"integrity":"sha512-LjgdTytVFXeUgtHZr9WYViYSM/g8MkcTPYDlPa3cDqMirHjKiSZPYd6DoL7pK8AJQr+uWkQvCjHNdiMqsrJs+g==","shasum":"e7078e38f231cb191855c3136a4423aef725d261","tarball":"http://registry-npm.ywwl.com/perfect-debounce/download/perfect-debounce-2.1.0.tgz","fileCount":5,"unpackedSize":9566,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEYCIQCyNOpDI/ZfV3pTi/bilbL/1P62TRDCUXNWTTnOO8nz6QIhAP01XpXNEC1tC90PptYlrq6FMl8THXmeLUbp/Xalv6nD"}],"size":3779,"noattachment":false,"key":"/perfect-debounce/-/perfect-debounce-2.1.0.tgz"},"_npmUser":{"name":"pi0","email":"pyapar@gmail.com"},"directories":{},"maintainers":[{"name":"pi0","email":"pyapar@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/perfect-debounce_2.1.0_1769039253320_0.49426584142687435"},"_hasShrinkwrap":false,"readme":"# perfect-debounce\n\n<!-- automd:badges color=yellow codecov bundlephobia packagephobia  -->\n\n[![npm version](https://img.shields.io/npm/v/perfect-debounce?color=yellow)](https://npmjs.com/package/perfect-debounce)\n[![npm downloads](https://img.shields.io/npm/dm/perfect-debounce?color=yellow)](https://npm.chart.dev/perfect-debounce)\n[![bundle size](https://img.shields.io/bundlephobia/minzip/perfect-debounce?color=yellow)](https://bundlephobia.com/package/perfect-debounce)\n[![install size](https://badgen.net/packagephobia/install/perfect-debounce?color=yellow)](https://packagephobia.com/result?p=perfect-debounce)\n[![codecov](https://img.shields.io/codecov/c/gh/unjs/perfect-debounce?color=yellow)](https://codecov.io/gh/unjs/perfect-debounce)\n\n<!-- /automd -->\n\nImproved debounce function with Promise support.\n\n## Features\n\n- Well tested debounce implementation\n- Native Promise support\n- Avoid duplicate calls while promise is being resolved\n- Configurable `trailing` and `leading` behavior\n- Control methods\n\n## Usage\n\nInstall package:\n\n```sh\nnpx nypm i perfect-debounce\n```\n\nImport:\n\n```js\nimport { debounce } from \"perfect-debounce\";\n```\n\nDebounce function:\n\n```js\nconst debounced = debounce(async () => {\n  // Some heavy stuff\n}, 25);\n```\n\nWhen calling `debounced`, it will wait at least for `25ms` as configured before actually calling your function. This helps to avoid multiple calls.\n\n### Control Methods\n\nThe returned debounced function provides additional control methods:\n\n- `debounced.cancel()`: Cancel any pending invocation that has not yet occurred.\n- `await debounced.flush()`: Immediately invoke the pending function call (if any) and return its result.\n- `debounced.isPending()`: Returns `true` if there is a pending invocation waiting to be called, otherwise `false`.\n\n```js\ndebounced.cancel(); // Cancel any pending call\nawait debounced.flush(); // Immediately invoke pending call (if any)\ndebounced.isPending(); // Returns true if a call is pending\n```\n\n### Example\n\n```js\nconst debounced = debounce(async (value) => {\n  // Some async work\n  return value * 2;\n}, 100);\n\ndebounced(1);\ndebounced(2);\ndebounced(3);\n\n// Check if a call is pending\nconsole.log(debounced.isPending()); // true\n\n// Immediately invoke the pending call\nconst result = await debounced.flush();\nconsole.log(result); // 6\n\n// Cancel any further pending calls\ndebounced.cancel();\n```\n\nTo avoid initial wait, we can set `leading: true` option. It will cause function to be immediately called if there is no other call:\n\n```js\nconst debounced = debounce(\n  async () => {\n    // Some heavy stuff\n  },\n  25,\n  { leading: true },\n);\n```\n\nIf 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:\n\n```js\nconst debounced = debounce(\n  async () => {\n    // Some heavy stuff\n  },\n  25,\n  { trailing: false },\n);\n```\n\n## 💻 Development\n\n- Clone this repository\n- Enable [Corepack](https://github.com/nodejs/corepack) using `corepack enable` (use `npm i -g corepack` for Node.js < 16.10)\n- Install dependencies using `pnpm install`\n- Run interactive tests using `pnpm dev`\n\n## License\n\nBased on [sindresorhus/p-debounce](https://github.com/sindresorhus/p-debounce).\n\nMade with 💛 Published under [MIT License](./LICENSE).\n","publish_time":1769039253457,"_cnpm_publish_time":1769039253457,"dist-tags":{"latest":"2.1.0"}}