🔰make iterator iterable

JSiterationiterator ⟩ make iterator iterable

an iterator can be made iterable by making it return itself in the make-iterator method.

const iterator = {

  // ⭐️ iterator must have `next()` method
  next() {
    // return iteration result ...
  },
  
  // ⭐️ iterable must have "make iterator" method
  [Symbol.iterator] () { 
      // because `iterator` is itself an iterator,
      // we can simply return itself. ⭐️ 
      return this;
  },
  
};

Last updated