LearnFrenzy
Sign in Take a free test

Event Loop · Find the Error · Question 13

Bug: the app reports "not ready" Answer, explanation & discussion

  • Find the error
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 13 Back to Exercise

The code below runs without an error, but its result is wrong. Which statement correctly identifies the bug?

main.js
let ready = false;
setTimeout(() => { ready = true; }, 0);           // "initialise first"
Promise.resolve().then(() => {
  console.log(ready ? "starting app" : "not ready");
});
  • A The promise callback is a microtask and runs before the zero-delay timer – do the initialisation synchronously or chain the start on the initialisation promise Correct
  • B ready must be declared with var
  • C The ternary operator is reversed
  • D then callbacks cannot read outer variables
Correct Answer: Option A

Never rely on setTimeout(…, 0) to run "before" other async work; express the dependency explicitly with promises.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links