JS: Iterator Interface

By Xah Lee. Date: . Last updated: .

Object having Iterator Interface is called Iterator. Iterator is used as function to general next values.

Iterator Interface Properties

"next"
  • If a previous call to the next method of an Iterator has returned an IteratorResult object whose done property is true, then all subsequent calls to the next method of that object should also return an IteratorResult object whose done property is true. However, this requirement is not enforced.

Iterator Interface Optional Property Keys

The following are optional

"return"
  • Invoking this method notifies the Iterator object that the caller does not intend to make any more next method calls to the Iterator.
  • The returned IteratorResult object will typically have a done property whose value is true, and a value property with the value passed as the argument of the return method. However, this requirement is not enforced.
"throw"
  • Invoking this method notifies the Iterator object that the caller has detected an error condition.
  • The argument may be used to identify the error condition and typically will be an exception object.
  • A typical response is to throw the value passed as the argument.
  • If the method does not throw, the returned IteratorResult object will typically have a done property whose value is true.

JavaScript, Iterable