2021-04-27
2239
#vanilla javascript
Felix Gerschau
45401
Apr 27, 2021 â‹… 7 min read

JavaScript iterators and generators: A complete guide

Felix Gerschau Felix is a frontend developer at Fitogram in Cologne, Germany.

Recent posts:

how to hide scrollbars with CSS

How to use CSS to hide scrollbars without impacting scrolling

Learn how to hide the scrollbar in popular web browsers by making use of modern CSS techniques, and dive into some interactive examples.

Fimber Elemuwa
Mar 4, 2025 â‹… 10 min read

16 React component libraries and kits for improved UI

Discover 16 of the most useful React content libraries and kits, and learn how to use them in your next React app.

Chidume Nnamdi
Mar 4, 2025 â‹… 16 min read
typescript vs javascript

TypeScript vs. JavaScript: Differences and use cases for each

Choosing between TypeScript and JavaScript depends on your project’s complexity, team structure, and long-term goals.

Kayode Adeniyi
Mar 4, 2025 â‹… 4 min read
Understanding UUIDs In Node.js

Understanding UUIDs in Node.js

Generate and validate UUIDs in Node.js using libraries like `uuid`, `short-uuid`, and `nanoid`, covering UUID versions and best practices.

Ukeje Goodness
Mar 3, 2025 â‹… 7 min read
View all posts

5 Replies to "JavaScript iterators and generators: A complete guide"

  1. Nice article. I was a little confused by the output shown for the `throw` example, so I tried running a modified version it in the browser.

    “`
    function* errorGenerator() {
    try {
    yield ‘one’;
    yield ‘two’;
    } catch(e) {
    console.error(e);
    }
    yield ‘three’;
    yield ‘four’;
    }

    const errorIterator = errorGenerator();

    console.log(errorIterator.next()); // outputs “{ value: ‘one’, done: false }”
    console.log(errorIterator.throw(‘Bam!’)); // outputs “Bam!” AND “{ value: ‘three’, done: false }”
    console.log(errorIterator.next()); // outputs “{ value: ‘four’, done: false }”
    console.log(errorIterator.next()); // outputs “{ value: undefined, done: true }”
    “`

    It appears that the throw doesn’t actually end the generator, but rather simulate an exception thrown, which is caught by the catch block, then continues the rest of the function normally.

Leave a Reply