LearnFrenzy
Sign in Take a free test

Event Loop · Find the Error · Question 3

Error: yield inside a timer callback Answer, explanation & discussion

  • Find the error
  • Hard
  • 0 comments
  • Answer explained
  • Moderated community
Question 3 Back to Exercise

JavaScript refuses to run the code below. Which statement correctly describes the problem?

main.js
function* ticker() {
  setTimeout(() => {
    yield "tick";
  }, 100);
}
  • A It yields "tick" after 100 ms
  • B SyntaxError: yield is only allowed directly in the generator body, not in a nested arrow function Correct
  • C It yields undefined
  • D TypeError when ticker() is called
Correct Answer: Option B

A generator cannot yield from a callback that runs later. Use an async generator that awaits a timer promise and then yields.

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