7.4 Operations on Iterator Objects
See Common Iteration Interfaces (
7.4.1 GetIterator ( obj [ , hint [ , method ] ] )
The abstract operation GetIterator takes argument obj and optional arguments hint and method. It performs the following steps when called:
- If hint is not present, set hint to
sync . Assert : hint is eithersync orasync .- If method is not present, then
- If hint is
async , then- Set method to ?
GetMethod (obj,@@asyncIterator ). - If method is
undefined , then- Let syncMethod be ?
GetMethod (obj,@@iterator ). - Let syncIteratorRecord be ?
GetIterator (obj,sync , syncMethod). - Return !
CreateAsyncFromSyncIterator (syncIteratorRecord).
- Let syncMethod be ?
- Set method to ?
- Otherwise, set method to ?
GetMethod (obj,@@iterator ).
- If hint is
- Let iterator be ?
Call (method, obj). - If
Type (iterator) is not Object, throw aTypeError exception. - Let nextMethod be ?
GetV (iterator,"next" ). - Let iteratorRecord be the
Record { [[Iterator]]: iterator, [[NextMethod]]: nextMethod, [[Done]]:false }. - Return iteratorRecord.
7.4.2 IteratorNext ( iteratorRecord [ , value ] )
The abstract operation IteratorNext takes argument iteratorRecord and optional argument value. It performs the following steps when called:
7.4.3 IteratorComplete ( iterResult )
The abstract operation IteratorComplete takes argument iterResult. It performs the following steps when called:
7.4.4 IteratorValue ( iterResult )
The abstract operation IteratorValue takes argument iterResult. It performs the following steps when called:
7.4.5 IteratorStep ( iteratorRecord )
The abstract operation IteratorStep takes argument iteratorRecord. It requests the next value from iteratorRecord.[[Iterator]] by calling iteratorRecord.[[NextMethod]] and returns either
- Let result be ?
IteratorNext (iteratorRecord). - Let done be ?
IteratorComplete (result). - If done is
true , returnfalse . - Return result.
7.4.6 IteratorClose ( iteratorRecord, completion )
The abstract operation IteratorClose takes arguments iteratorRecord and completion. It is used to notify an iterator that it should perform any actions it would normally perform when it has reached its completed state. It performs the following steps when called:
Assert :Type (iteratorRecord.[[Iterator]]) is Object.Assert : completion is aCompletion Record .- Let iterator be iteratorRecord.[[Iterator]].
- Let innerResult be
GetMethod (iterator,"return" ). - If innerResult.[[Type]] is
normal , then- Let return be innerResult.[[Value]].
- If return is
undefined , returnCompletion (completion). - Set innerResult to
Call (return, iterator).
- If completion.[[Type]] is
throw , returnCompletion (completion). - If innerResult.[[Type]] is
throw , returnCompletion (innerResult). - If
Type (innerResult.[[Value]]) is not Object, throw aTypeError exception. - Return
Completion (completion).
7.4.7 AsyncIteratorClose ( iteratorRecord, completion )
The abstract operation AsyncIteratorClose takes arguments iteratorRecord and completion. It is used to notify an async iterator that it should perform any actions it would normally perform when it has reached its completed state. It performs the following steps when called:
Assert :Type (iteratorRecord.[[Iterator]]) is Object.Assert : completion is aCompletion Record .- Let iterator be iteratorRecord.[[Iterator]].
- Let innerResult be
GetMethod (iterator,"return" ). - If innerResult.[[Type]] is
normal , then- Let return be innerResult.[[Value]].
- If return is
undefined , returnCompletion (completion). - Set innerResult to
Call (return, iterator). - If innerResult.[[Type]] is
normal , set innerResult toAwait (innerResult.[[Value]]).
- If completion.[[Type]] is
throw , returnCompletion (completion). - If innerResult.[[Type]] is
throw , returnCompletion (innerResult). - If
Type (innerResult.[[Value]]) is not Object, throw aTypeError exception. - Return
Completion (completion).
7.4.8 CreateIterResultObject ( value, done )
The abstract operation CreateIterResultObject takes arguments value and done. It creates an object that supports the IteratorResult interface. It performs the following steps when called:
Assert :Type (done) is Boolean.- Let obj be !
OrdinaryObjectCreate (%Object.prototype% ). - Perform !
CreateDataPropertyOrThrow (obj,"value" , value). - Perform !
CreateDataPropertyOrThrow (obj,"done" , done). - Return obj.
7.4.9 CreateListIteratorRecord ( list )
The abstract operation CreateListIteratorRecord takes argument list. It creates an Iterator (
- Let closure be a new
Abstract Closure with no parameters that captures list and performs the following steps when called:- For each element E of list, do
- Perform ?
Yield (E).
- Perform ?
- Return
undefined .
- For each element E of list, do
- Let iterator be !
CreateIteratorFromClosure (closure,empty ,%IteratorPrototype% ). - Return
Record { [[Iterator]]: iterator, [[NextMethod]]: %GeneratorFunction.prototype.prototype.next%, [[Done]]:false }.
The list iterator object is never directly accessible to ECMAScript code.
7.4.10 IterableToList ( items [ , method ] )
The abstract operation IterableToList takes argument items and optional argument method. It performs the following steps when called:
- If method is present, then
- Let iteratorRecord be ?
GetIterator (items,sync , method).
- Let iteratorRecord be ?
- Else,
- Let iteratorRecord be ?
GetIterator (items,sync ).
- Let iteratorRecord be ?
- Let values be a new empty
List . - Let next be
true . - Repeat, while next is not
false ,- Set next to ?
IteratorStep (iteratorRecord). - If next is not
false , then- Let nextValue be ?
IteratorValue (next). - Append nextValue to the end of the
List values.
- Let nextValue be ?
- Set next to ?
- Return values.