What is @types/debounce-promise?
@types/debounce-promise provides TypeScript type definitions for the debounce-promise package, which is used to debounce promises. Debouncing is a technique to limit the rate at which a function is executed, ensuring that it is only called once within a specified time frame, even if it is triggered multiple times.
What are @types/debounce-promise's main functionalities?
Basic Debouncing
This feature allows you to debounce a promise-returning function. In this example, the fetchData function is debounced with a delay of 2000 milliseconds. Even if debouncedFetchData is called multiple times, the fetchData function will only be executed once within the 2-second window.
const debounce = require('debounce-promise');
const fetchData = () => {
return new Promise((resolve) => {
setTimeout(() => {
resolve('Data fetched');
}, 1000);
});
};
const debouncedFetchData = debounce(fetchData, 2000);
debouncedFetchData().then(console.log); // 'Data fetched' after 2 seconds
Immediate Execution
This feature allows you to execute the debounced function immediately on the leading edge of the timeout. In this example, the fetchData function is executed immediately when debouncedFetchData is called, and subsequent calls within the 2-second window will be ignored.
const debounce = require('debounce-promise');
const fetchData = () => {
return new Promise((resolve) => {
setTimeout(() => {
resolve('Data fetched');
}, 1000);
});
};
const debouncedFetchData = debounce(fetchData, 2000, { leading: true });
debouncedFetchData().then(console.log); // 'Data fetched' immediately
Cancel Debounced Function
This feature allows you to cancel a debounced function before it gets executed. In this example, the debouncedFetchData function is canceled before it can execute the fetchData function.
const debounce = require('debounce-promise');
const fetchData = () => {
return new Promise((resolve) => {
setTimeout(() => {
resolve('Data fetched');
}, 1000);
});
};
const debouncedFetchData = debounce(fetchData, 2000);
debouncedFetchData().then(console.log); // 'Data fetched' after 2 seconds
debouncedFetchData.cancel(); // Cancels the debounced function
Other packages similar to @types/debounce-promise
lodash
Lodash is a popular utility library that provides a wide range of functions for common programming tasks, including debouncing. The debounce function in Lodash can be used to debounce any function, not just promises. It offers more configuration options compared to debounce-promise.
underscore
Underscore is another utility library similar to Lodash, providing a variety of functional programming helpers. It also includes a debounce function that can be used to limit the rate at which a function is executed. Like Lodash, it is not limited to debouncing promises.
p-debounce
p-debounce is a small utility specifically designed for debouncing promise-returning functions. It is similar to debounce-promise but offers a simpler API and is focused solely on debouncing promises.
Installation
npm install --save @types/debounce-promise
Summary
This package contains type definitions for debounce-promise (https://github.com/bjoerge/debounce-promise).
Details
Files were exported from https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/debounce-promise.
declare namespace debounce {
interface DebounceOptions {
leading?: boolean | undefined;
accumulate?: boolean | undefined;
}
}
declare function debounce<T extends any[], R>(
func: (args: Array<[...T]>) => R,
wait?: number,
options?: debounce.DebounceOptions & { accumulate: true },
): (
...args: T
) => R extends Promise<any> ? R
: Promise<R>;
declare function debounce<T extends (...args: any[]) => any>(
func: T,
wait?: number | (() => number),
options?: debounce.DebounceOptions,
): (
...args: Parameters<T>
) => ReturnType<T> extends Promise<any> ? ReturnType<T>
: Promise<ReturnType<T>>;
export = debounce;
Additional Details
- Last updated: Mon, 06 Nov 2023 22:41:05 GMT
- Dependencies: none
Credits
These definitions were written by Wu Haotian, and Trevor Robinson.