generator using higher order functions, not using yield

PHOTO EMBED

Sat Oct 21 2023 04:50:25 GMT+0000 (Coordinated Universal Time)

Saved by @sadik #javascript #generator #higher-order-function

function el(arr) {
  let i = 0;
  return function gen() {
    if (i < arr.length) {
      let val = arr[i];
      i += 1;
      return val;
    }
  }
}
const myArray = [1, 2, 3];
const myGenerator = el(myArray);

console.log(myGenerator()); // 1
console.log(myGenerator()); // 2
console.log(myGenerator()); // 3
console.log(myGenerator()); // undefined
content_copyCOPY

video: https://youtu.be/99Zacm7SsWQ?si=DMjv-7OOlInonYd9&t=1398