{"name":"perfect-debounce","version":"2.0.0","license":"MIT","_id":"perfect-debounce@2.0.0","maintainers":[{"name":"pi0","email":"pyapar@gmail.com"}],"homepage":"https://github.com/unjs/perfect-debounce#readme","bugs":{"url":"https://github.com/unjs/perfect-debounce/issues"},"dist":{"shasum":"0ff94f1ecbe0a6bca4b1703a2ed08bbe43739aa7","tarball":"http://registry-npm.ywwl.com/perfect-debounce/download/perfect-debounce-2.0.0.tgz","fileCount":5,"integrity":"sha512-fkEH/OBiKrqqI/yIgjR92lMfs2K8105zt/VT6+7eTjNwisrsh47CeIED9z58zI7DfKdH3uHAn25ziRZn3kgAow==","signatures":[{"sig":"MEYCIQCa1lzx5+0lSgkHy2XQb6qPPdki/l0ifK+hCs13e7CqBwIhAOX11VuuACnNGGSoQxaQ+90RTI7184P8ZSLaBbsXH9W/","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":9545,"size":3775,"noattachment":false,"key":"/perfect-debounce/-/perfect-debounce-2.0.0.tgz"},"main":"./dist/index.mjs","type":"module","types":"./dist/index.d.mts","module":"./dist/index.mjs","exports":{".":"./dist/index.mjs"},"gitHead":"d872850a230611e4972ecd7ee86784fb0aafe866","scripts":{"dev":"vitest dev","lint":"eslint . && prettier --check src test","test":"vitest run --coverage","build":"obuild","release":"pnpm test && pnpm build && changelogen --release --push && npm publish","lint:fix":"eslint . --fix && prettier -w src test"},"_npmUser":{"name":"pi0","email":"pyapar@gmail.com"},"repository":{"url":"git+https://github.com/unjs/perfect-debounce.git","type":"git"},"_npmVersion":"10.9.3","description":"<!-- automd:badges color=yellow codecov bundlephobia packagephobia  -->","directories":{},"sideEffects":false,"_nodeVersion":"22.18.0","_hasShrinkwrap":false,"packageManager":"pnpm@10.15.0","devDependencies":{"automd":"^0.4.0","eslint":"^9.34.0","obuild":"^0.2.1","vitest":"^3.2.4","in-range":"^3.0.0","prettier":"^3.6.2","time-span":"^5.1.0","typescript":"^5.9.2","@types/node":"^24.3.0","changelogen":"^0.6.2","eslint-config-unjs":"^0.5.0","@vitest/coverage-v8":"^3.2.4"},"_npmOperationalInternal":{"tmp":"tmp/perfect-debounce_2.0.0_1755897854774_0.6973565376216238","host":"s3://npm-registry-packages-npm-production"},"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":1755897854956,"_cnpm_publish_time":1755897854956,"dist-tags":{"latest":"2.1.0"}}