Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

How does an es6 iterator in javascript work?


Asked by Giuliana Hill on Dec 05, 2021 JavaScript



This iterator will have a method called next which will return an object with keys value and done. The value key will contain the current value. It can be of any type. The done is boolean. It denotes whether all the values have been fetched or not.
Consequently,
ES6 - Iterator 1 Introduction to Iterator. Iterator is an object which allows us to access a collection of objects one at a time. ... 2 Custom Iterable. Certain types in JavaScript are iterable (E.g. Array, Map etc.) while others are not (E.g. ... 3 Generator. Prior to ES6, functions in JavaScript followed a run-to completion model. ...
In addition, ES6 introduced a new way of working with functions and iterators in the form of Generators (or generator functions). A generator is a function that can stop midway and then continue from where it stopped. In short, a generator appears to be a function but it behaves like an iterator.
In fact,
This function returns an iterator object. The iterator object has a function next which returns an object {value:'customer',done:true/false}. Prior to ES6, functions in JavaScript followed a run-to completion model. ES6 introduces functions known as Generator which can stop midway and then continue from where it stopped.
Subsequently,
It is also possible to iterate a collection of DOM elements like a NodeList. You might be looking at the first code example and thinking it is pretty verbose with some boiler code. ES6 has some syntax sugar to help make this all look clean and succinct. An example below of a generator: The key features to defining a generator are function*.