Description

Debouncing is delaying an API call by X ms so that we make only one call once the user is finished typing.
?

Reach for when

The user does rapid actions that cancel each other out.

Code

function debounce(fn, delay) {
  let timer;
  return function (...args) {
    clearTimeout(timer);
    timer = setTimeout(() => fn.apply(this, args), delay);
  };
}