// 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)
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter