MCPcopy Index your code
hub / github.com/chodorowicz/ts-debounce

github.com/chodorowicz/ts-debounce @v5.0.1

Chat with this repo
repository ↗ · DeepWiki ↗ · release v5.0.1 ↗ · + Follow
8 symbols 16 edges 3 files 0 documented · 0% 2 cross-repo links updated 2mo agov5.0.1 · 2026-04-21★ 1402 open issues
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

TypeScript implementation of debounce function

Build Status npm npm bundle size (minified + gzip) npm type definitions

All Contributors

Debounce creates a new function g, which when called will delay the invocation of the original function f until n milliseconds, BUT drop previous pending delayed emissions if a new invocation is made before n milliseconds.

It's very useful for scenarios when it's better to limit the number of times the function is called. E.g. think of search input which fetches data from API. It's enough display search results after user has stopped entering characters for some time.

Install

You can install this package using npm using the command below

npm install ts-debounce

If you prefer yarn, install using the command below

yarn add ts-debounce

Function arguments

import { debounce } from "ts-debounce";

const debouncedFunction = debounce(originalFunction, waitMilliseconds, options);
  • originalFunction
  • the function which we want to debounce
  • waitMilliseconds
  • how many seconds must pass after most recent function call, for the original function to be called
  • options
  • isImmediate (boolean)
    • if set to true then originalFunction will be called immediately, but on subsequent calls of the debounced function original function won't be called, unless waitMilliseconds passed after last call
  • maxWait (number)
    • if defined it will call the originalFunction after maxWait time has passed, even if the debounced function is called in the meantime
    • e.g. if wait is set to 100 and maxWait to 200, then if the debounced function is called every 50ms, then the original function will be called after 200ms anyway
  • callback (function)
    • it is called when originalFunction is debounced and receives as first parameter returned data from originalFunction

Cancellation

The returned debounced function can be cancelled by calling cancel() on it.

const debouncedFunction = debounce(originalFunction, waitMilliseconds, options);

debouncedFunction.cancel();

Promises

Since v3 ts-debounce has Promise support. Everytime you call debounced function a promise is returned which will be resolved when the original function will be finally called. This promise will be rejected, if the debounced function will be cancelled.

You can also debounce a function f which returns a promise. The returned promise(s) will resolve (unless cancelled) with the return value of the original function.

const asyncFunction = async () => "value";
const g = debounce(asyncFunction);
const returnValue = await g();
returnValue === "value"; // true

Credits & inspiration

This implementation is based upon following sources:

Compability

  • version 5 - TypeScript 3.5 (use of Awaited type)
  • version 3 - Promise must be available in the global namespace
  • version 2 - TypeScript 3.3
  • version 1 - TypeScript 2.0

Contributors ✨

Thanks goes to these wonderful people (emoji key):

Zac Zac 💻 Karol Majewski Karol Majewski 💻 Fabien Rogeret Fabien Rogeret 💻 Iman Iman 💻 juanmendes juanmendes 🤔 sanduluca sanduluca 💻 Robert Taylor Robert Taylor 💻

This project follows the all-contributors specification. Contributions of any kind welcome!

Extension points exported contracts — how you extend this code

DebouncedFunction (Interface)
(no doc)
src/index.ts
DebouncedPromise (Interface)
(no doc)
src/index.ts

Core symbols most depended-on inside this repo

debounce
called by 17
src/index.ts
debouncedFunction
called by 17
src/index.ts
nextInvokeTimeout
called by 1
src/index.ts
testInference
called by 1
src/index.test-d.ts
invokeFunction
called by 0
src/index.ts

Shape

Function 6
Interface 2

Languages

TypeScript100%

Modules by API surface

src/index.ts6 symbols
src/index.test.ts1 symbols
src/index.test-d.ts1 symbols

Used by 2 indexed graphs manifest dependencies, hub-wide

For agents

$ claude mcp add ts-debounce \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact