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 withReturn(a) { | |
| let b = 5; | |
| return a + b; | |
| b = 6; // we will never re-assign b | |
| return a * b; // and will never return new value | |
| } | |
| withReturn(6); // 11 | |
| withReturn(6); // 11 |
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
| class MyClass { | |
| *generator() {} | |
| * generator() {} | |
| } | |
| const obj = { | |
| *generator() {} | |
| * generator() {} | |
| } |
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 () {} | |
| function* generator () {} | |
| function *generator () {} | |
| let generator = function * () {} | |
| let generator = function* () {} | |
| let generator = function *() {} | |
| let generator = *() => {} // SyntaxError | |
| let generator = ()* => {} // SyntaxError |
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 * generatorForLoop(num) { | |
| for (let i = 0; i < num; i += 1) { | |
| yield console.log(i); | |
| } | |
| } | |
| const genForLoop = generatorForLoop(5); | |
| genForLoop.next(); // first console.log - 0 | |
| genForLoop.next(); // 1 |
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
| for (let i = 0; i < 5; i += 1) { | |
| console.log(i); | |
| } | |
| // this will return immediately 0 -> 1 -> 2 -> 3 -> 4 |
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 i in arr) { | |
| yield i; | |
| yield yield; | |
| yield(yield); | |
| } | |
| } | |
| const gen = generator([0,1]); |
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 * randomFrom(...arr) { | |
| while (true) | |
| yield arr[Math.floor(Math.random() * arr.length)]; | |
| } | |
| const getRandom = randomFrom(1, 2, 5, 9, 4); | |
| getRandom.next().value; // returns random value |
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 * iterateArray(arr) { | |
| for (let i in arr) | |
| yield arr[i]; | |
| } | |
| const iterArr = iterateArray([0,1,2]); | |
| iterArr.next(); // {value: 0, done: false} | |
| iterArr.next(); // {value: 1, done: false} | |
| iterArr.next(); // {value: 2, done: false} | |
| iterArr.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 * fibonacci(seed1, seed2) { | |
| while (true) { | |
| yield (() => { | |
| seed2 = seed2 + seed1; | |
| seed1 = seed2 - seed1; | |
| return seed2; | |
| })(); | |
| } | |
| } |
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 * throttle(func, time) { | |
| let timerID = null; | |
| function throttled(arg) { | |
| clearTimeout(timerID); | |
| timerID = setTimeout(func.bind(window, arg), time); | |
| } | |
| while (true) | |
| throttled(yield); | |
| } |