Question 13
Back to Exercise
The code below runs without an error, but its result is wrong. Which statement correctly identifies the bug?
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!