Skip to content

Commit

Permalink
feat: top-level-for-await (denoland#3212)
Browse files Browse the repository at this point in the history
  • Loading branch information
hayd authored and ry committed Oct 27, 2019
1 parent 51dd91a commit aec5a64
Show file tree
Hide file tree
Showing 18 changed files with 257 additions and 263 deletions.
3 changes: 2 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
cli/tests/error_syntax.js
cli/tests/badly_formatted.js
cli/tests/top_level_for_await.js
std/**/testdata
std/**/vendor
std/node_modules
std/node_modules
3 changes: 3 additions & 0 deletions cli/js/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,9 @@ window.compilerMain = function compilerMain(): void {

diagnostics = ts.getPreEmitDiagnostics(program).filter(
({ code }): boolean => {
// TS1103: 'for-await-of' statement is only allowed within an async
// function or async generator.
if (code === 1103) return false;
// TS1308: 'await' expression is only allowed within an async
// function.
if (code === 1308) return false;
Expand Down
8 changes: 2 additions & 6 deletions cli/tests/045_proxy_client.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
async function main(): Promise<void> {
const res = await fetch("https://localhost:4545/std/examples/colors.ts");
console.log(`Response http: ${await res.text()}`);
}

main();
const res = await fetch("https://localhost:4545/std/examples/colors.ts");
console.log(`Response http: ${await res.text()}`);
12 changes: 4 additions & 8 deletions cli/tests/045_proxy_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,7 @@ async function testModuleDownload(): Promise<void> {
http.close();
}

async function main(): Promise<void> {
proxyServer();
await testFetch();
await testModuleDownload();
Deno.exit(0);
}

main();
proxyServer();
await testFetch();
await testModuleDownload();
Deno.exit(0);
10 changes: 10 additions & 0 deletions cli/tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -597,3 +597,13 @@ itest!(top_level_await_ts {
args: "--allow-read top_level_await.ts",
output: "top_level_await.out",
});

itest!(top_level_for_await {
args: "top_level_for_await.js",
output: "top_level_for_await.out",
});

itest!(top_level_for_await_ts {
args: "top_level_for_await.ts",
output: "top_level_for_await.out",
});
10 changes: 10 additions & 0 deletions cli/tests/top_level_for_await.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
async function* asyncGenerator() {
let i = 0;
while (i < 3) {
yield i++;
}
}

for await (const num of asyncGenerator()) {
console.log(num);
};
3 changes: 3 additions & 0 deletions cli/tests/top_level_for_await.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
0
1
2
10 changes: 10 additions & 0 deletions cli/tests/top_level_for_await.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
async function* asyncGenerator(): AsyncIterableIterator<number> {
let i = 0;
while (i < 3) {
yield i++;
}
}

for await (const num of asyncGenerator()) {
console.log(num);
}
13 changes: 5 additions & 8 deletions std/examples/cat.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
async function cat(filenames: string[]): Promise<void> {
for (const filename of filenames) {
const file = await Deno.open(filename);
await Deno.copy(Deno.stdout, file);
file.close();
}
const filenames = Deno.args.slice(1);
for (const filename of filenames) {
const file = await Deno.open(filename);
await Deno.copy(Deno.stdout, file);
file.close();
}

cat(Deno.args.slice(1));
46 changes: 21 additions & 25 deletions std/examples/catj.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,32 +79,28 @@ function print(data: any): void {
}
}

async function main(): Promise<void> {
const parsedArgs = parse(Deno.args.slice(1));

if (parsedArgs.h || parsedArgs.help || parsedArgs._.length === 0) {
console.log("Usage: catj [-h|--help] [file...]");
console.log();
console.log("Examples:");
console.log();
console.log("// print file:\n catj file.json");
console.log();
console.log("// print multiple files:\n catj file1.json file2.json");
console.log();
console.log("// print from stdin:\n cat file.json | catj -");
}
const parsedArgs = parse(Deno.args.slice(1));

if (parsedArgs.h || parsedArgs.help || parsedArgs._.length === 0) {
console.log("Usage: catj [-h|--help] [file...]");
console.log();
console.log("Examples:");
console.log();
console.log("// print file:\n catj file.json");
console.log();
console.log("// print multiple files:\n catj file1.json file2.json");
console.log();
console.log("// print from stdin:\n cat file.json | catj -");
}

if (parsedArgs._[0] === "-") {
const contents = await Deno.readAll(Deno.stdin);
const json = JSON.parse(decoder.decode(contents));
if (parsedArgs._[0] === "-") {
const contents = await Deno.readAll(Deno.stdin);
const json = JSON.parse(decoder.decode(contents));
print(json);
} else {
for (const fileName of parsedArgs._) {
const fileContents = await Deno.readFile(fileName);
const json = JSON.parse(decoder.decode(fileContents));
print(json);
} else {
for (const fileName of parsedArgs._) {
const fileContents = await Deno.readFile(fileName);
const json = JSON.parse(decoder.decode(fileContents));
print(json);
}
}
}

main();
10 changes: 3 additions & 7 deletions std/examples/curl.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
async function curl(url: string): Promise<void> {
const res = await fetch(url);
await Deno.copy(Deno.stdout, res.body);
}

await curl(Deno.args[1]);
Deno.exit(0);
const url = Deno.args[1];
const res = await fetch(url);
await Deno.copy(Deno.stdout, res.body);
9 changes: 3 additions & 6 deletions std/http/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,11 @@ console.log("Set-Cookie:", cookieHeader);
```typescript
import { serve } from "https://deno.land/std/http/server.ts";
const s = serve("0.0.0.0:8000");
const body = new TextEncoder().encode("Hello World\n");

async function main() {
for await (const req of s) {
req.respond({ body: new TextEncoder().encode("Hello World\n") });
}
for await (const req of s) {
req.respond({ body });
}

main();
```

### File Server
Expand Down
10 changes: 4 additions & 6 deletions std/http/testdata/simple_server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@
// This is an example of a server that responds with an empty body
import { serve } from "../server.ts";

window.onload = async function main() {
const addr = "0.0.0.0:4502";
console.log(`Simple server listening on ${addr}`);
for await (const req of serve(addr)) {
req.respond({});
}
const addr = "0.0.0.0:4502";
console.log(`Simple server listening on ${addr}`);
for await (const req of serve(addr)) {
req.respond({});
}
10 changes: 4 additions & 6 deletions std/manual.md
Original file line number Diff line number Diff line change
Expand Up @@ -937,12 +937,10 @@ Example:

import { serve } from "http/server.ts";

window.onload = async function() {
const body = new TextEncoder().encode("Hello World\n");
for await (const req of serve(":8000")) {
req.respond({ body });
}
};
const body = new TextEncoder().encode("Hello World\n");
for await (const req of serve(":8000")) {
req.respond({ body });
}
```

```shell
Expand Down
Loading

0 comments on commit aec5a64

Please sign in to comment.