From 420cf390d9e294c111f1957713cfd36138d0cd9c Mon Sep 17 00:00:00 2001 From: Rumyra Date: Tue, 23 Mar 2021 13:09:26 +0000 Subject: [PATCH 1/4] expression --- ...pressions-asyncfunctionasteriskexpression.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 live-examples/js-examples/expressions/expressions-asyncfunctionasteriskexpression.js diff --git a/live-examples/js-examples/expressions/expressions-asyncfunctionasteriskexpression.js b/live-examples/js-examples/expressions/expressions-asyncfunctionasteriskexpression.js new file mode 100644 index 000000000..8a9a3e834 --- /dev/null +++ b/live-examples/js-examples/expressions/expressions-asyncfunctionasteriskexpression.js @@ -0,0 +1,17 @@ +async function* foo() { + 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" From 3f7df76396b7122c0fbddc584ecdbccb3fde0a91 Mon Sep 17 00:00:00 2001 From: Rumyra Date: Tue, 23 Mar 2021 13:58:43 +0000 Subject: [PATCH 2/4] statement --- .../statement-asyncfunctionasterisk.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 live-examples/js-examples/statement/statement-asyncfunctionasterisk.js diff --git a/live-examples/js-examples/statement/statement-asyncfunctionasterisk.js b/live-examples/js-examples/statement/statement-asyncfunctionasterisk.js new file mode 100644 index 000000000..4c8b9f081 --- /dev/null +++ b/live-examples/js-examples/statement/statement-asyncfunctionasterisk.js @@ -0,0 +1,17 @@ +async function* asyncGenerator() { + yield await Promise.resolve(1); + yield await Promise.resolve(2); + yield await Promise.resolve(3); +} + +const asyncGen = asyncGenerator(); + +asyncGen.next() + .then(res => console.log(res.value)); + // expected output: 1 +asyncGen.next() + .then(res => console.log(res.value)); + // expected output: 2 +asyncGen.next() + .then(res => console.log(res.value)); + // expected output: 3 From 15967f4389462dac49d5a422240d9e5a6754ec5e Mon Sep 17 00:00:00 2001 From: Rumyra Date: Tue, 23 Mar 2021 14:05:41 +0000 Subject: [PATCH 3/4] fixing --- .../statement/statement-asyncfunctionasterisk.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/live-examples/js-examples/statement/statement-asyncfunctionasterisk.js b/live-examples/js-examples/statement/statement-asyncfunctionasterisk.js index 4c8b9f081..0d14e0cca 100644 --- a/live-examples/js-examples/statement/statement-asyncfunctionasterisk.js +++ b/live-examples/js-examples/statement/statement-asyncfunctionasterisk.js @@ -8,10 +8,12 @@ const asyncGen = asyncGenerator(); asyncGen.next() .then(res => console.log(res.value)); - // expected output: 1 +// expected output: 1 + asyncGen.next() .then(res => console.log(res.value)); - // expected output: 2 +// expected output: 2 + asyncGen.next() .then(res => console.log(res.value)); - // expected output: 3 +// expected output: 3 From 83b707f271538827a67f8be06c6d9851a450849f Mon Sep 17 00:00:00 2001 From: Rumyra Date: Wed, 14 Jul 2021 12:37:35 +0100 Subject: [PATCH 4/4] tidying up --- ...=> expressions-async-function-asterisk.js} | 0 .../js-examples/expressions/meta.json | 24 ++++++++------ .../statement-async-function-asterisk.js | 32 +++++++++---------- .../statement-asyncfunctionasterisk.js | 19 ----------- 4 files changed, 31 insertions(+), 44 deletions(-) rename live-examples/js-examples/expressions/{expressions-asyncfunctionasteriskexpression.js => expressions-async-function-asterisk.js} (100%) delete mode 100644 live-examples/js-examples/statement/statement-asyncfunctionasterisk.js diff --git a/live-examples/js-examples/expressions/expressions-asyncfunctionasteriskexpression.js b/live-examples/js-examples/expressions/expressions-async-function-asterisk.js similarity index 100% rename from live-examples/js-examples/expressions/expressions-asyncfunctionasteriskexpression.js rename to live-examples/js-examples/expressions/expressions-async-function-asterisk.js diff --git a/live-examples/js-examples/expressions/meta.json b/live-examples/js-examples/expressions/meta.json index 9f2a81493..91983b2df 100644 --- a/live-examples/js-examples/expressions/meta.json +++ b/live-examples/js-examples/expressions/meta.json @@ -1,23 +1,29 @@ { "pages": { - "expressionsAddition": { + "expressionsAddition": { "exampleCode": "./live-examples/js-examples/expressions/expressions-addition.js", "fileName": "expressions-addition.html", "title": "JavaScript Demo: Expressions - Addition operator", "type": "js" - }, - "expressionsAdditionAssignment": { - "exampleCode": "./live-examples/js-examples/expressions/expressions-addition-assignment.js", - "fileName": "expressions-addition-assignment.html", - "title": "JavaScript Demo: Expressions - Addition assignment operator", - "type": "js" - }, - "expressionsAssignment": { + }, + "expressionsAdditionAssignment": { + "exampleCode": "./live-examples/js-examples/expressions/expressions-addition-assignment.js", + "fileName": "expressions-addition-assignment.html", + "title": "JavaScript Demo: Expressions - Addition assignment operator", + "type": "js" + }, + "expressionsAssignment": { "exampleCode": "./live-examples/js-examples/expressions/expressions-assignment.js", "fileName": "expressions-assignment.html", "title": "JavaScript Demo: Expressions - Assignment", "type": "js" }, + "expressionsAsyncFunctionAsterisk": { + "exampleCode": "./live-examples/js-examples/expressions/expressions-async-function-asterisk.js", + "fileName": "expressions-async-function-asterisk.html", + "title": "JavaScript Demo: Expressions - Async Function Asterisk", + "type": "js" + }, "expressionsBitwiseAND": { "exampleCode": "./live-examples/js-examples/expressions/expressions-bitwise-and.js", "fileName": "expressions-bitwise-and.html", diff --git a/live-examples/js-examples/statement/statement-async-function-asterisk.js b/live-examples/js-examples/statement/statement-async-function-asterisk.js index 771aa926d..0d14e0cca 100644 --- a/live-examples/js-examples/statement/statement-async-function-asterisk.js +++ b/live-examples/js-examples/statement/statement-async-function-asterisk.js @@ -1,19 +1,19 @@ -async function* randomNumbers() { - const url = 'https://www.random.org/decimal-fractions/?num=1&dec=10&col=1&format=plain&rnd=new'; - - console.log('Print randomly generated numbers until reset is clicked, or the random number exceeds 0.95'); - while (true) { - const response = await fetch(url); - const text = await response.text(); - yield Number(text); - } +async function* asyncGenerator() { + yield await Promise.resolve(1); + yield await Promise.resolve(2); + yield await Promise.resolve(3); } -async function printRandoms() { - for await (const number of randomNumbers()) { - console.log(number); - if (number > 0.95) { break; } - } -} +const asyncGen = asyncGenerator(); + +asyncGen.next() + .then(res => console.log(res.value)); +// expected output: 1 + +asyncGen.next() + .then(res => console.log(res.value)); +// expected output: 2 -printRandoms(); +asyncGen.next() + .then(res => console.log(res.value)); +// expected output: 3 diff --git a/live-examples/js-examples/statement/statement-asyncfunctionasterisk.js b/live-examples/js-examples/statement/statement-asyncfunctionasterisk.js deleted file mode 100644 index 0d14e0cca..000000000 --- a/live-examples/js-examples/statement/statement-asyncfunctionasterisk.js +++ /dev/null @@ -1,19 +0,0 @@ -async function* asyncGenerator() { - yield await Promise.resolve(1); - yield await Promise.resolve(2); - yield await Promise.resolve(3); -} - -const asyncGen = asyncGenerator(); - -asyncGen.next() - .then(res => console.log(res.value)); -// expected output: 1 - -asyncGen.next() - .then(res => console.log(res.value)); -// expected output: 2 - -asyncGen.next() - .then(res => console.log(res.value)); -// expected output: 3