A function type that returns a Promise.
The async function to debounce.
The debounce interval in milliseconds.
A new function that wraps fn
with debounce behavior.
Each call returns a Promise that resolves to the result of fn
.
const fetchData = async (id: number) => {
console.log('Fetching', id);
return id * 2;
};
const debouncedFetch = debouncePromise(fetchData, 200);
debouncedFetch(1).then(console.log); // Will log 2
debouncedFetch(2).then(console.log); // Will also log 4 after 200ms, previous call resolves with 4 too
Debounces a promise-returning function.
This function ensures that the provided async function
fn
is only invoked once perwait
milliseconds, regardless of how many times the returned function is called within that period. All calls within the wait period will resolve with the same result of the last invocation.