Skip to content
engineering workbench.by StudyPort Academy
Learning path
Advanced20 minLESSON 28 / 34

Iterators & generators

01

Understand the idea

A custom collection stores values correctly, but for...of says it is not . Storage alone does not define how should proceed.

A cursor through results

Some work should happen only when another value is requested. You might read a sequence in small pieces or stop as soon as you find what you need. Iterators let the consumer ask for one more result without requiring the producer to build an entire first.

The next result has two parts

An iterable supplies a at Symbol.iterator that returns an iterator. Arrays and strings already implement this protocol, which is why for...of can consume them. An ordinary object is not iterable just because it has ; use an appropriate collection of its entries when that is what you need.

Calling next returns an object with value and done. A value of alone does not mean the sequence is finished: undefined can be a legitimate yielded value. The done flag is what tells you whether the iterator has completed.

The parts of an on-demand sequence
Term or operationMeaningA useful distinction
IteratorHas a next() operation returning value and doneRepresents one progression through results
IterableProvides an iterator through Symbol.iteratorCan be consumed by for...of or spread
Generator function (function*)Creates a generator objectIts body runs as the consumer requests results
yieldOffers one value and pausesDoes not finish the generator
return in a generatorFinishes it with a final resultdone is true; for...of does not yield that final value

Iterable versus iterator

A generator function, written with function*, creates an iterator when called. Its body does not run to completion immediately. A call to next starts or resumes it until yield pauses and supplies a value. A return finishes it with done set to true.

A generator pauses at yield

Generators make lazy sequences readable. Values are produced when requested rather than all stored upfront. This can reduce intermediate allocation, but it does not make expensive computation . Work between yields still runs on the current thread.

Do not assume a cursor restarts

An iterator may be exhausted after one traversal. A generator function can create another fresh iterator, while reusing the finished iterator produces no more values. Make infinite sequences explicit and give consumers a stopping rule; spreading an infinite iterator into an array never finishes.

Calling a generator function creates a generator object; it does not immediately execute the whole body. The first next starts it, each yield pauses it, and the next request resumes after that yield. Once this generator finishes, further next calls stay done. Call the generator function again when you want a fresh independent cursor.

02

Mental model

FOLLOW THE VALUE

ticketNumbers()
create cursor
body paused

No yielded values have been requested yet.

01 / 03Trace the transformation

03

Live example

Two next calls receive ticket values. The third confirms completion instead of producing a third ticket.

Opening editor…
LIVE PREVIEW
Console 0

Your console output appears here.

Saved on this deviceJavaScript · ES2022
04

Guided challenge

Yield the second value

Keep the generator open long enough to yield 102 as a normal iteration value.

Opening editor…
LIVE PREVIEW
Console 0

Your console output appears here.

Saved on this deviceJavaScript · ES2022

EXPECTED CONSOLE OUTPUT

101
102
false
true

Hints 0/2

    Use all hints or make 3 failed runs (0/3).

    05

    Free sandbox

    Make a variation, test an assumption, or break the example on purpose. This draft is saved separately.

    Opening editor…
    LIVE PREVIEW
    Console 0

    Your console output appears here.

    Saved on this deviceJavaScript · ES2022
    06

    Quiz

    A quick check for understanding. Retry as often as you like; your learning path stays open.

    01When does a generator body begin executing?
    02Does yield move work onto another thread?

    Ready to call this one understood?

    Mark this lesson complete to earn 200 XP. You can always revisit it.

    Sign in to save progress
    Next lesson