From 2315bc6a8f9e8f0dc09cff73c88b2d85f7854523 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Niedzi=C3=B3=C5=82ka?= Date: Thu, 8 Dec 2022 17:09:56 +0100 Subject: [PATCH] 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 Co-authored-by: Joshua Chen --- .../async-functionasterisk-function.js | 19 +++++++++++++++++++ .../functionasterisk-function.js | 15 +++++++++++++++ .../js-examples/functionasterisk/meta.json | 16 ++++++++++++++++ 3 files changed, 50 insertions(+) create mode 100644 live-examples/js-examples/functionasterisk/async-functionasterisk-function.js create mode 100644 live-examples/js-examples/functionasterisk/functionasterisk-function.js create mode 100644 live-examples/js-examples/functionasterisk/meta.json diff --git a/live-examples/js-examples/functionasterisk/async-functionasterisk-function.js b/live-examples/js-examples/functionasterisk/async-functionasterisk-function.js new file mode 100644 index 000000000..1705d6abc --- /dev/null +++ b/live-examples/js-examples/functionasterisk/async-functionasterisk-function.js @@ -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" diff --git a/live-examples/js-examples/functionasterisk/functionasterisk-function.js b/live-examples/js-examples/functionasterisk/functionasterisk-function.js new file mode 100644 index 000000000..7cc7bd426 --- /dev/null +++ b/live-examples/js-examples/functionasterisk/functionasterisk-function.js @@ -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" diff --git a/live-examples/js-examples/functionasterisk/meta.json b/live-examples/js-examples/functionasterisk/meta.json new file mode 100644 index 000000000..1024e7d6f --- /dev/null +++ b/live-examples/js-examples/functionasterisk/meta.json @@ -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" + } + } +}