Working with Function Generators and Symbols.

PHOTO EMBED

Tue Apr 22 2025 18:22:15 GMT+0000 (Coordinated Universal Time)

Saved by @fsd

// Generator function
function* countUpTo(max) {
    let count = 1;
    while (count <= max) {
      yield count;  // Pause and return the current count
      count++;
    }
  }
  
  // Using the generator
  const counter = countUpTo(3);
  console.log(counter.next().value);  // Output: 1
  console.log(counter.next().value);  // Output: 2
  console.log(counter.next().value);  // Output: 3
  console.log(counter.next().value);  // Output: undefined (no more values)
  
content_copyCOPY