🚨 JavaScript Async Interview Question What will be the output? function debounce(fn, delay) { let timer; return function (value) { clearTimeout(timer); timer = setTimeout(() => { fn(value); }, delay); }; } const log = debounce((v) => console.log(v), 100); log("A"); setTimeout(() => log("B"), 50); setTimeout(() => log("C"), 120); setTimeout(() => log("D"), 180); This question tests your understanding of: • Debounce behavior • setTimeout scheduling • How timers get cancelled and replaced • JavaScript async execution Many developers assume all values will print, but debounce changes the execution flow. What do you think the output will be? #JavaScript #FrontendInterview #Debounce #AsyncJavaScript #ReactJS #ProductBasedCompany
Due to debounce behaviour, as the next log arrives within 100ms of the current call, it cancels the current timer. So, output - D
D
Hint: Debounce cancels the previous timer every time a new call happens within the delay window.