LearnFrenzy
Sign in Take a free test

Callbacks · Find the Error · Question 12

Bug: the result is undefined Answer, explanation & discussion

  • Find the error
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 12 Back to Exercise

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

main.js
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!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links