-
Notifications
You must be signed in to change notification settings - Fork 510
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add JS GeneratorFunction examples (#2341)
* JS GeneratorFunction examples * Lint fixes * Update live-examples/js-examples/functionasterisk/async-functionasterisk-function.js Co-authored-by: Joshua Chen <sidachen2003@gmail.com> Co-authored-by: Joshua Chen <sidachen2003@gmail.com>
- Loading branch information
1 parent
056a7f2
commit 2315bc6
Showing
3 changed files
with
50 additions
and
0 deletions.
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
live-examples/js-examples/functionasterisk/async-functionasterisk-function.js
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
const AsyncGeneratorFunction = async function* () {}.constructor; | ||
|
||
const foo = new AsyncGeneratorFunction(` | ||
yield await Promise.resolve('a'); | ||
yield await Promise.resolve('b'); | ||
yield await Promise.resolve('c'); | ||
`); | ||
|
||
let str = ''; | ||
|
||
async function generate() { | ||
for await (const val of foo()) { | ||
str = str + val; | ||
} | ||
console.log(str); | ||
} | ||
|
||
generate(); | ||
// expected output: "abc" |
15 changes: 15 additions & 0 deletions
15
live-examples/js-examples/functionasterisk/functionasterisk-function.js
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
const GeneratorFunction = function* () {}.constructor; | ||
|
||
const foo = new GeneratorFunction(` | ||
yield 'a'; | ||
yield 'b'; | ||
yield 'c'; | ||
`); | ||
|
||
let str = ''; | ||
for (const val of foo()) { | ||
str = str + val; | ||
} | ||
|
||
console.log(str); | ||
// expected output: "abc" |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"pages": { | ||
"asyncFunctionAsteriskFunction": { | ||
"exampleCode": "./live-examples/js-examples/functionasterisk/async-functionasterisk-function.js", | ||
"fileName": "async-functionasterisk-function.html", | ||
"title": "JavaScript Demo: AsyncGeneratorFunction()", | ||
"type": "js" | ||
}, | ||
"functionAsteriskFunction": { | ||
"exampleCode": "./live-examples/js-examples/functionasterisk/functionasterisk-function.js", | ||
"fileName": "functionasterisk-function.html", | ||
"title": "JavaScript Demo: GeneratorFunction()", | ||
"type": "js" | ||
} | ||
} | ||
} |