index.d.ts 1.2 KB

12345678910111213141516171819202122232425262728293031323334
  1. interface DebounceOptions {
  2. /**
  3. Call the `fn` on the [leading edge of the timeout](https://css-tricks.com/debouncing-throttling-explained-examples/#article-header-id-1).
  4. Meaning immediately, instead of waiting for `wait` milliseconds.
  5. @default false
  6. */
  7. readonly leading?: boolean;
  8. /**
  9. Call the `fn` on trailing edge with last used arguments. Result of call is from previous call.
  10. @default false
  11. */
  12. readonly trailing?: boolean;
  13. }
  14. /**
  15. Debounce functions
  16. @param fn - Promise-returning/async function to debounce.
  17. @param wait - Milliseconds to wait before calling `fn`. Default value is 25ms
  18. @returns A function that delays calling `fn` until after `wait` milliseconds have elapsed since the last time it was called.
  19. @example
  20. ```
  21. import { debounce } from 'perfect-debounce';
  22. const expensiveCall = async input => input;
  23. const debouncedFn = debounce(expensiveCall, 200);
  24. for (const number of [1, 2, 3]) {
  25. console.log(await debouncedFn(number));
  26. }
  27. //=> 3
  28. //=> 3
  29. //=> 3
  30. ```
  31. */
  32. declare function debounce<ArgumentsT extends unknown[], ReturnT>(fn: (...args: ArgumentsT) => PromiseLike<ReturnT> | ReturnT, wait?: number, options?: DebounceOptions): (...args: ArgumentsT) => Promise<ReturnT>;
  33. export { DebounceOptions, debounce };