This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function * generator(arr) { | |
| for (const el in arr) | |
| yield el; | |
| } | |
| const gen = generator([0, 1, 2]); | |
| for (const g of gen) { | |
| console.log(g); // 0 -> 1 -> 2 | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function * generator() { | |
| yield 1; | |
| return 2; | |
| yield 3; // we will never reach this yield | |
| } | |
| const gen = generator(); | |
| gen.next(); // {value: 1, done: false} | |
| gen.next(); // {value: 2, done: true} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function * anotherGenerator(i) { | |
| yield i + 1; | |
| yield i + 2; | |
| yield i + 3; | |
| } | |
| function * generator(i) { | |
| yield* anotherGenerator(i); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const strings = document.querySelectorAll('.string'); | |
| const btn = document.querySelector('#btn'); | |
| const className = 'darker'; | |
| function * addClassToEach(elements, className) { | |
| for (const el of Array.from(elements)) | |
| yield el.classList.add(className); | |
| } | |
| const addClassToStrings = addClassToEach(strings, className); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function * generator() { | |
| yield 1; | |
| yield 2; | |
| yield 3; | |
| } | |
| const gen = generator(); | |
| gen.throw('Something bad'); // Error Uncaught Something bad | |
| gen.next(); // {value: undefined, done: true} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function * generator() { | |
| yield 1; | |
| yield 2; | |
| yield 3; | |
| } | |
| const gen = generator(); | |
| gen.return(); // {value: undefined, done: true} | |
| gen.return('Heeyyaa'); // {value: "Heeyyaa", done: true} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function * generator() { | |
| yield 1; | |
| yield 2; | |
| yield 3; | |
| } | |
| const gen = generator(); | |
| gen.next(); // {value: 1, done: false} | |
| gen.next(); // {value: 2, done: false} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function * generator(arg = 'Nothing') { | |
| yield arg; | |
| } | |
| const gen0 = generator(); // OK | |
| const gen1 = generator('Hello'); // OK | |
| const gen2 = new generator(); // Not OK | |
| generator().next(); // It will work, but every time from the beginning |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function * generator() { | |
| yield 5; | |
| } | |
| const gen = generator(); | |
| gen.next(); // {value: 5, done: false} | |
| gen.next(); // {value: undefined, done: true} | |
| gen.next(); // {value: undefined, done: true} - all other calls will produce the same result |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function * withYield(a) { | |
| let b = 5; | |
| yield a + b; | |
| b = 6; // it will be re-assigned after first execution | |
| yield a * b; | |
| } | |
| const calcSix = withYield(6); | |
| calcSix.next().value; // 11 |