Skip to content

Commit

Permalink
Add JS GeneratorFunction examples (#2341)
Browse files Browse the repository at this point in the history
* 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
NiedziolkaMichal and Josh-Cena authored Dec 8, 2022
1 parent 056a7f2 commit 2315bc6
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
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"
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"
16 changes: 16 additions & 0 deletions live-examples/js-examples/functionasterisk/meta.json
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"
}
}
}

0 comments on commit 2315bc6

Please sign in to comment.