Question 12
Back to Exercise
The code below runs without an error, but its result is wrong. Which statement correctly identifies the bug?
let price;
function fetchPrice(cb) {
setTimeout(() => cb(499), 0);
}
fetchPrice((p) => { price = p; });
console.log("Price:", price);
- A setTimeout loses the value
- B price must be a const
- C The callback must return p
- D The callback runs later, so price has not been set yet when console.log runs – use the value inside the callback Correct
Correct Answer: Option D
Asynchronous results are only available in (or after) the callback: fetchPrice((p) => console.log("Price:", p)).
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!