Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Async generator examples #1784

Merged
merged 5 commits into from
Jul 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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"
24 changes: 15 additions & 9 deletions live-examples/js-examples/expressions/meta.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
Original file line number Diff line number Diff line change
@@ -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