This file contains 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
console.log([...genFnSimple()]); // [1,2] | |
const [first, second] = genFnInfinite(); | |
console.log(first); // 1 | |
console.log(second); // 2 |
This file contains 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* genFnInfinite() { | |
let value = 0; | |
while (true) { | |
value++; | |
yield value; | |
} | |
} | |
const genObj = genFnInfinite(); | |
console.log(genObj.next()); // { value: 1, done: false } |
This file contains 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* genFnSimple() { | |
yield 1; | |
yield 2; | |
return 3; | |
} | |
for (const value of genFnSimple()) { | |
console.log(value); | |
// 1 | |
// 2 |
This file contains 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* genFnArray() { | |
yield* [1, 2, 3]; | |
} | |
const genObj = genFnArray(); | |
console.log(genObj.next()); | |
console.log(genObj.next()); | |
console.log(genObj.next()); |
This file contains 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 genObjA = genFnSimple(); | |
console.log(genObjA.return()); // { value: undefined, done: true } | |
const genObjB = genFnSimple(); | |
try { | |
genObjA.throw(new Error('test')) | |
} catch (error) { | |
console.log(error); // Uncaught Error: test | |
} |
This file contains 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* genFnRecursiveBad() { | |
yield 0; | |
const genInner = genFnSimple(); | |
const one = genInner.next(); | |
yield one.value; | |
const two = genInner.next(); | |
yield two.value; | |
const three = genInner.next(); | |
yield three.value; | |
return 4; |
This file contains 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* genFnRecursive() { | |
yield 0; | |
const result = yield* genFnSimple(); | |
yield result; | |
return 4; | |
} | |
const genObj = genFnRecursive(); | |
console.log(genObj.next()); // { value: 0, done: false } | |
console.log(genObj.next()); // { value: 1, done: false } |
This file contains 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* genFnTwoWay() { | |
// 0 is lost | |
const received = yield 1; | |
return received; | |
} | |
const genObj = genFnTwoWay(); | |
console.log(genObj.next(0)); // { value: 1, done: false } | |
console.log(genObj.next(2)); // { value: 2, done: true } |
This file contains 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* genFnSimple() { | |
yield 1; | |
yield 2; | |
return 3; | |
} | |
const genObj = genFnSimple(); | |
console.log(genObj.next()); // { value: 1, done: false } | |
console.log(genObj.next()); // { value: 2, done: false } | |
console.log(genObj.next()); // { value: 3, done: true } |
This file contains 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 asyncOptional = async (withWait) => { | |
if (!withWait) { | |
return true; | |
} | |
const result = await waitForABit(1000); | |
return result; | |
}; | |
const asyncTest = async () => { |
NewerOlder