# Generators, the forgotten spec— A JavaScript journey #8

> In a time just before the rise of the Promises, they came and fell to oblivion in a blink. Sadly nobody uses them, including me, but generators are still in \[…\]

Source: https://antistatique.net/en/blog/generators-the-forgotten-spec-a-javascript-journey-8

Category: Development

# Generators, the forgotten spec— A JavaScript journey #8

Author(s)

[

![Yann](https://images.statique.ch/t/w_750,h_750,c_fill,f_jpeg,q_70/wp/app/uploads/2020/12/Yann-G-300x300.jpg)



](/en/agence/yann)

[Yann](/en/agence/yann)25 April 2019

In a time just before the rise of the Promises, they came and fell to oblivion in a blink. Sadly nobody uses them, including me, but **generators are still in the game** (or not). Maybe you remember this strange `yield` expression or the tiny `*` declaration to transform your dummy function into a beautiful generator.

So yes, you can use `Promise`, `async/await`, almost any `Array` methods or even recursions to do exactly what generators can do AND without the need of knowing them. It's a fact and I'm the first to approach my code this way, but maybe, we can take a closer look and try to use all the goodness that JavaScript as to offer, generators included.

## A simple form

Add a star (`*`) to your function declaration, multiple returns (`yield`) and voilà, you just have created a generator!

```
const generator = function* () {
  yield 1;
  yield 2;
  yield 3;
};

const counter = generator();
console.log(counter.next()); // {value: 1, done: false}
console.log(counter.next()); // {value: 2, done: false}
console.log(counter.next()); // {value: 3, done: false}
console.log(counter.next()); // {value: undefined, done: true}
```

## A simple API

There are only three methods attached to the Generator's prototype :

-   `.next()`: return the next “yielded” value
-   `.return()`: return the given value and force the generator to end (kill switch)
-   `.throw()`: throw an error to the generator and finish it in a more _errorish_ way

## When to use it ?

Generators are very useful **when you need a method that you can replay on demand with some kind of inner evolution**. Something not as autonomous as a recursive method and not as dummy as a multi-parameters function. In summary, an iterator.

In the following example, we want to add a new item to our list when clicking on a button, but with a maximum of `10` items :

```
const itemMaker = function* ({ max }) {
  for (let i = 0; i <= max; i++) {
    const item = document.createElement('li');
    item.innerHTML = `item #${i}`;
    yield item;
  }
};

const items = itemMaker({ max: 10 });
const list = document.getElementById('list');
const button = document.getElementById('btn');

button.addEventListener('click', () => {
  const newItem = items.next();
  if (!newItem.done) list.appendChild(newItem.value);
});
```

It illustrates how cool generator can be in that case. You can **keep the iteration logic inside** your method, no need to keep track of the current or last index in an external variable. Simple, clear, elegant, plus the useful `done` value to check the progress.

## Conclusion

Generators are maybe not the most used JavaScript expression, but still, there are cases where they can be very handy. See it as a special type of iterator. Now, it's your call to choose if you prefer generators over the very appealing `async/await` syntax 😇

## More articles

Author(s)

[

![Anthony](https://images.statique.ch/t/w_750,h_750,c_fill,f_jpeg,q_70/wp/app/uploads/2024/11/anthony-1-300x300.png)



](/en/agence/anthony-salamone)

[Anthony](/en/agence/anthony-salamone)28 September 2026

### [Smashing Conference Freiburg 2026](/en/blog/smashing-conference-freiburg-2026)

Back in Freiburg for Smashing Conference 2026: AI and design systems, deeply technical CSS and HTML, and more human talks about teams and mindset. Here's what we took away.

Back in Freiburg for Smashing Conference 2026: AI and design systems, deeply technical CSS and HTML, and more human talks about teams and mindset. Here's what we took away.

[

![Smashing Conference Freiburg 2026 poster](https://images.statique.ch/t/w_750,h_500,c_fill,f_jpeg,q_70/assets/blog/smashing-2026.png)



](/en/blog/smashing-conference-freiburg-2026)

Author(s)

[

![Akira](https://images.statique.ch/t/w_750,h_750,c_fill,f_jpeg,q_70/wp/app/uploads/2025/03/akira-small-300x300.jpg)



](/en/agence/akira-kamata)

[Akira](/en/agence/akira-kamata)22 July 2026

### [Making of : “La nuit des Losers”](/en/blog/making-of-la-nuit-des-losers)

lanuitdeslosers.ch, built fast: paper wireframes, a bold hand-drawn identity, then Claude Code to prototype and ship. A mouse-tracking hero animation, a bowling game made of sponsor logos, and a few bugs we’re keeping.

lanuitdeslosers.ch, built fast: paper wireframes, a bold hand-drawn identity, then Claude Code to prototype and ship. A mouse-tracking hero animation, a bowling game made of sponsor logos, and a few bugs we’re keeping.

[

![Making of : “La nuit des Losers”](https://images.statique.ch/t/w_750,h_500,c_fill,f_jpeg,q_70/wp/app/uploads/2026/05/La-nuit-des-Losers-Teaser-1-768x576.png)



](/en/blog/making-of-la-nuit-des-losers)

Author(s)

[

![Pierre](https://images.statique.ch/t/w_750,h_750,c_fill,f_jpeg,q_70/wp/app/uploads/2020/12/Pierre-300x300.jpg)



](/en/agence/pierre)

[Pierre](/en/agence/pierre)7 July 2026

### [Dark mode: necessity or trend](/en/blog/dark-mode-necessity-or-trend)

Nous faisons le tri entre idées reçues, réalités techniques et bonnes pratiques de design.

Nous faisons le tri entre idées reçues, réalités techniques et bonnes pratiques de design.

[

![Dark mode: necessity or trend](https://images.statique.ch/t/w_750,h_500,c_fill,f_jpeg,q_70/wp/app/uploads/2026/07/1-768x384.png)



](/en/blog/dark-mode-necessity-or-trend)
