feat: top-level-for-await (#3212)

This commit is contained in:
Andy Hayden 2019-10-27 06:04:42 -07:00 committed by Ry Dahl
parent 51dd91a3cc
commit aec5a646c9
18 changed files with 257 additions and 263 deletions

View file

@ -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

View file

@ -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;

View file

@ -1,7 +1,3 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
async function main(): Promise<void> {
const res = await fetch("http://localhost:4545/std/examples/colors.ts");
console.log(`Response http: ${await res.text()}`);
}
main();

View file

@ -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();

View file

@ -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",
});

View file

@ -0,0 +1,10 @@
async function* asyncGenerator() {
let i = 0;
while (i < 3) {
yield i++;
}
}
for await (const num of asyncGenerator()) {
console.log(num);
};

View file

@ -0,0 +1,3 @@
0
1
2

View file

@ -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);
}

View file

@ -1,10 +1,7 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
async function cat(filenames: string[]): Promise<void> {
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));

View file

@ -79,7 +79,6 @@ 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) {
@ -105,6 +104,3 @@ async function main(): Promise<void> {
print(json);
}
}
}
main();

View file

@ -1,8 +1,4 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
async function curl(url: string): Promise<void> {
const url = Deno.args[1];
const res = await fetch(url);
await Deno.copy(Deno.stdout, res.body);
}
await curl(Deno.args[1]);
Deno.exit(0);

View file

@ -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") });
req.respond({ body });
}
}
main();
```
### File Server

View file

@ -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({});
}
}

View file

@ -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 });
}
};
```
```shell

View file

@ -15,8 +15,8 @@ import {
WebSocket
} from "https://deno.land/std/ws/mod.ts";
/** websocket echo server */
const port = Deno.args[1] || "8080";
async function main(): Promise<void> {
console.log(`websocket server is running on :${port}`);
for await (const req of serve(`:${port}`)) {
const { headers, conn } = req;
@ -66,11 +66,6 @@ async function main(): Promise<void> {
}
);
}
}
if (import.meta.main) {
main();
}
```
### Client
@ -88,7 +83,7 @@ import { TextProtoReader } from "https://deno.land/std/textproto/mod.ts";
import { blue, green, red, yellow } from "https://deno.land/std/fmt/colors.ts";
const endpoint = Deno.args[1] || "ws://127.0.0.1:8080";
async function main(): Promise<void> {
/** simple websocket cli */
const sock = await connectWebSocket(endpoint);
console.log(green("ws connected! (type 'close' to quit)"));
(async function(): Promise<void> {
@ -104,6 +99,7 @@ async function main(): Promise<void> {
}
}
})();
const tpr = new TextProtoReader(new BufReader(Deno.stdin));
while (true) {
await Deno.stdout.write(encode("> "));
@ -119,15 +115,17 @@ async function main(): Promise<void> {
} else {
await sock.send(line);
}
await new Promise((resolve): number => setTimeout(resolve, 0));
// FIXME: Without this,
// sock.receive() won't resolved though it is readable...
await new Promise(
(resolve): void => {
setTimeout(resolve, 0);
}
);
}
await sock.close(1000);
// FIXME: conn.close() won't shutdown process...
Deno.exit(0);
}
if (import.meta.main) {
main();
}
```
## API

View file

@ -11,7 +11,6 @@ import { blue, green, red, yellow } from "../fmt/colors.ts";
const endpoint = Deno.args[1] || "ws://127.0.0.1:8080";
/** simple websocket cli */
async function main(): Promise<void> {
const sock = await connectWebSocket(endpoint);
console.log(green("ws connected! (type 'close' to quit)"));
(async function(): Promise<void> {
@ -27,6 +26,7 @@ async function main(): Promise<void> {
}
}
})();
const tpr = new TextProtoReader(new BufReader(Deno.stdin));
while (true) {
await Deno.stdout.write(encode("> "));
@ -53,8 +53,3 @@ async function main(): Promise<void> {
await sock.close(1000);
// FIXME: conn.close() won't shutdown process...
Deno.exit(0);
}
if (import.meta.main) {
main();
}

View file

@ -9,7 +9,6 @@ import {
/** websocket echo server */
const port = Deno.args[1] || "8080";
async function main(): Promise<void> {
console.log(`websocket server is running on :${port}`);
for await (const req of serve(`:${port}`)) {
const { headers, conn } = req;
@ -59,8 +58,3 @@ async function main(): Promise<void> {
}
);
}
}
if (import.meta.main) {
main();
}

View file

@ -5,13 +5,6 @@ const addr = Deno.args[1] || "127.0.0.1:4500";
const originAddr = Deno.args[2] || "127.0.0.1:4501";
const server = serve(addr);
async function main(): Promise<void> {
console.log(`Proxy listening on http://${addr}/`);
for await (const req of server) {
proxyRequest(req);
}
}
async function proxyRequest(req: ServerRequest): Promise<void> {
const url = `http://${originAddr}${req.url}`;
const resp = await fetch(url, {
@ -21,4 +14,7 @@ async function proxyRequest(req: ServerRequest): Promise<void> {
req.respond(resp);
}
main();
console.log(`Proxy listening on http://${addr}/`);
for await (const req of server) {
proxyRequest(req);
}