Generator & Iterators - Iterable [MDN Documentation]

When I read about iterators and generator in MDN. I read some paragraph in there and I don't understand what exactly this mean? Anyone can help me what is this paragraph mean exactly?

It may be possible to iterate over an iterable more than once, or only once. It is up to the programmer to know which is the case. Iterables which can iterate only once (e.g. Generators) customarily return this from their @@iterator method, where those which can be iterated many times must return a new iterator on each invocation of @@iterator.

Have a look at this example:

function iterateTwice(iterable) {
    for (const x of iterable) console.log(1, x);
    for (const x of iterable) console.log(2, x);
}

console.log("array");
const array = ['a', 'b', 'c'];
iterateTwice(array);

console.log("generator");
function* makeGenerator() {
    yield 'a'; yield 'b'; yield 'c';
}
const generator = makeGenerator();
iterateTwice(generator);

Trailer-mounted generators for rent 75kW - 2MW commercial & industrial. We have the model & size you need in stock for nationwide delivery!

It means, you could create a generator as you want. For example this eternity is returning the elements forever.

Be carefull, if you use something like [...eternity(array)].

function* eternity(array) {
    while (true) yield* array;
}

var array = [1, 2, 3],
    gen = eternity(array);

console.log(gen.next().value);
console.log(gen.next().value);
console.log(gen.next().value);
console.log(gen.next().value);
console.log(gen.next().value);
console.log(gen.next().value);
console.log(gen.next().value);
console.log(gen.next().value);

Browse Through Top Brands & Models. Free Shipping. Shop Now!

First of all

There is no @@iterator function, you have to use Symbol.iterator to implement iteration .

Now as you see the article discusses generators and iterable at the same time, that's because generators are iterable, and implement iteration behavior for a custom object could get describe with generators. Let's take a look at the MDN example :

var myIterable = {
    *[Symbol.iterator]() {
        yield 1;
        yield 2;
        yield 3;
    }
}

for (let value of myIterable) { 
    console.log(value); 
}
// 1
// 2
// 3

It creates iteration overobject with exact 3 itrates.

Writing the same iterable without using generators could explain it more about

return this from their @@iterator method

let range = {
  from: 1,
  to: 3,

  [Symbol.iterator]() {
    this.current = this.from;
    return this;
  },

  next() {
    if (this.current <= this.to) {
      return { done: false, value: this.current++ };
    } else {
      return { done: true };
    }
  }
};

for (let num of range) {
  console.log(num); // 1, 2, 3
}

Low Prices, Fast Shipping, Quality Industrial Parts, 30 Day Money Back

Receive Generator Installation Costs in Your Area. Enter Your Zip To Compare Estimates! Safe and Reputable Source of Licensed Electricians. Free & Easy, No Credit Card Required!

Portable generators are often used on job sites to power air compressors, nail guns, saws, hammer drills and other equipment. They’re also great as an emergency backup system for larger appliances. If noise is a concern, quiet generators may be your best bet.

There are three major types of generators. The first is the inverter generator, which is a smaller, more lightweight option that’s ideal for tailgating and RVing. If you want a quiet generator, this is the best option. Another generator type is a backup generator.

Comments
  • Thanks for comment. I understand what documentation mean. In there problem documentation say "this" and this is make confusing. I think someone must update documentation.
  • You mean the part "customarily return this from their @@iterator method"? It should read return this, because that's exactly how the generator[Symbol.iterator] method is implemented to return the generator object itself (which is an iterator).
  • Yeah I know what U say but I think this is hard to understand without someone's help. Because documentation not say before generator(iterator) have [Symbol.iterator] property. I think they must add some example.
  • Yeah I know we can make any type generator. Actually I don't understand what mean this part "Iterables which can iterate only once (e.g. Generators) customarily return this from their @@iterator method"
  • Yeah I understand what U mean but in there this part "Iterables which can iterate only once (e.g. Generators) customarily return this from their @@iterator method" hard understand for me.
  • To make it more clear know that It’s impossible to have two for..of loops running over the object simultaneously: they’ll share the iteration state because there’s only one iterator – the object itself . that's the this that we return from iterable at each iteration.
  • In the bellow comment @Bergi ansüer my question correctly. If U wantU can look him answer and thank U for help :)
  • It means that you can write an iterator which always returns the same value like yield 1; yield 2; .. or you can write an iterattor which doesn't, like current time and all the following minutes.