Delay a function call until the user stops typing to avoid unnecessary requests.
function debounce(callback, delay = 300) {
let timer;
return (...args) => {
clearTimeout(timer);
timer = setTimeout(() => callback(...args), delay);
};
}
const onSearch = debounce((query) => console.log(query));