Bird
0
0

Identify the problem in this code using recursive setTimeout:

medium📝 Debug Q14 of 15
Node.js - Timers and Scheduling
Identify the problem in this code using recursive setTimeout:
function repeat() {
  setTimeout(() => {
    console.log('Tick');
  }, 1000);
  repeat();
}
repeat();
AThe function calls itself immediately causing a stack overflow.
BThe timeout delay is too short to see output.
CThe console.log is outside the timeout callback.
DThe function never calls itself, so it runs only once.
Step-by-Step Solution
Solution:
  1. Step 1: Examine recursion timing

    The function repeat calls itself immediately after scheduling setTimeout, without waiting for the timeout to finish.
  2. Step 2: Identify consequence

    This causes infinite immediate recursion, leading to stack overflow before any timeout callback runs.
  3. Final Answer:

    The function calls itself immediately causing a stack overflow. -> Option A
  4. Quick Check:

    Immediate recursion without delay [OK]
Quick Trick: Call recursive function inside timeout callback only [OK]
Common Mistakes:
  • Placing recursive call outside timeout callback
  • Assuming timeout delays recursion automatically
  • Ignoring stack overflow risk

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes