ProgramGuru

Produce a Sequence on Demand

Produce a Sequence on Demand

A generator pauses at yield and continues on the next iteration request. It can describe a sequence of roll numbers without building the complete output array first.

Before this topic, review Read an Optional Property Safely if its terms are unfamiliar.

What you’ll learn

  • Explain produce a sequence on demand through the school example.
  • Trace the shown operations and distinguish their result from displayed output.
  • Make the practice change and explain its consequence: The second yielded value changes without changing the first.

Work through the school example

This is JavaScript using language features and the host console. Read the initial values, then follow each operation to its displayed result. A declaration names a value, a function call performs an operation, and console.log displays its arguments.

JavaScript · school.js · complete example

function* rolls() {
  yield 1;
  yield 2;
}
for (const roll of rolls()) console.log(roll);

Expected output

1
2
Run in JavaScript compiler →

Recognize the important boundary

Generators are consumed as they advance. A fresh call creates a new generator; asynchronous iteration has a different protocol.

Try a small variation

JavaScript · original source for practice

function* rolls() {
  yield 1;
  yield 2;
}
for (const roll of rolls()) console.log(roll);

Your task: Replace yield 2 with yield 3. Predict the result before running the edited program.

The source is repeated here. If the edit targets the HTML rather than the script, use the markup repeated below; the worked answer also repeats the changed markup.

Worked answer and reasoning

The second yielded value changes without changing the first.

JavaScript · complete worked variation

function* rolls() {
  yield 1;
  yield 3;
}
for (const roll of rolls()) console.log(roll);

Expected output

1
3
Run in JavaScript compiler →

Predict the original program

JavaScript · original program for this question

function* rolls() {
  yield 1;
  yield 2;
}
for (const roll of rolls()) console.log(roll);
What does the original JavaScript program display, in order?
Choose one answer. You can try again.

Answer and explanation

1 → 2

A generator pauses at yield and continues on the next iteration request. It can describe a sequence of roll numbers without building the complete output array first. Generators are consumed as they advance. A fresh call creates a new generator; asynchronous iteration has a different protocol.

Keep the useful idea

A generator pauses at yield and continues on the next iteration request. It can describe a sequence of roll numbers without building the complete output array first.

Check your variation against this explanation: The second yielded value changes without changing the first.

Use the JavaScript array reference library for individual array operations and boundary tests to check a rule independently.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Represent Dates and Time Explicitly →