Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ When the generator's [`next()`](/en-US/docs/Web/JavaScript/Reference/Global_Obje

When the generator's [`throw()`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Generator/throw) method is called, it acts as if a `throw` statement is inserted in the generator's body at the current suspended position. Similarly, when the generator's [`return()`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Generator/return) method is called, it acts as if a `return` statement is inserted in the generator's body at the current suspended position. Both methods usually finish the generator, unless the generator function catches the completion via {{jsxref("Statements/try...catch", "try...catch...finally")}}.

Generators used to be a paradigm for asynchronous programming, avoiding [Callback Hell](https://medium.com/@raihan_tazdid/callback-hell-in-javascript-all-you-need-to-know-296f7f5d3c1) by achieving [Inversion of Control](https://frontendmasters.com/courses/rethinking-async-js/callback-problems-inversion-of-control/). Nowadays, this use case is solved with the simpler [async functions](/en-US/docs/Web/JavaScript/Reference/Statements/async_function) model and the {{jsxref("Promise")}} object. However, generators are still useful for many other tasks, such as defining [iterators](/en-US/docs/Web/JavaScript/Guide/Iterators_and_generators) in a straightforward way.
Generators used to be a paradigm for asynchronous programming, avoiding [Callback Hell](https://medium.com/@raihan_tazdid/callback-hell-in-javascript-all-you-need-to-know-296f7f5d3c1) by achieving [Inversion of Control](https://en.wikipedia.org/wiki/Inversion_of_control). Nowadays, this use case is solved with the simpler [async functions](/en-US/docs/Web/JavaScript/Reference/Statements/async_function) model and the {{jsxref("Promise")}} object. However, generators are still useful for many other tasks, such as defining [iterators](/en-US/docs/Web/JavaScript/Guide/Iterators_and_generators) in a straightforward way.

`function*` declarations behave similar to {{jsxref("Statements/function", "function")}} declarations — they are [hoisted](/en-US/docs/Glossary/Hoisting) to the top of their scope and can be called anywhere in their scope, and they can be redeclared only in certain contexts.

Expand Down