diff --git a/1-js/12-generators-iterators/1-generators/article.md b/1-js/12-generators-iterators/1-generators/article.md index e77ceb66..1b8f0329 100644 --- a/1-js/12-generators-iterators/1-generators/article.md +++ b/1-js/12-generators-iterators/1-generators/article.md @@ -448,6 +448,28 @@ try { If we don't catch the error there, then, as usual, it falls through to the outer calling code (if any) and, if uncaught, kills the script. +## generator.return + +`generator.return(value)` finishes the generator execution and return the given `value`. + +```js +function* gen() { + yield 1; + yield 2; + yield 3; +} + +const g = gen(); + +g.next(); // { value: 1, done: false } +g.return('foo'); // { value: "foo", done: true } +g.next(); // { value: undefined, done: true } +``` + +If we again use `generator.return()` in a completed generator, it will return that value again ([MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Generator/return)). + +Often we don't use it, as most of time we want to get all returning values, but it can be useful when we want to stop generator in a condition. + ## Summary - Generators are created by generator functions `function* f(…) {…}`.