diff options
author | Andy Hayden <andyhayden1@gmail.com> | 2019-10-28 12:58:35 -0700 |
---|---|---|
committer | Ry Dahl <ry@tinyclouds.org> | 2019-10-28 15:58:35 -0400 |
commit | f484776384ad7df35ab7626b7a673f3902a6cfaa (patch) | |
tree | cb74f31550dc666ec75f5a2f95d61dc69931cb16 /std/examples | |
parent | 71efe6f2c530d1cb9e8a2679f5778e2c034a9d0d (diff) |
Use top-level for-await in various places (#3217)
Diffstat (limited to 'std/examples')
-rw-r--r-- | std/examples/curl.ts | 4 | ||||
-rw-r--r-- | std/examples/echo_server.ts | 3 | ||||
-rwxr-xr-x | std/examples/gist.ts | 96 |
3 files changed, 48 insertions, 55 deletions
diff --git a/std/examples/curl.ts b/std/examples/curl.ts index e020016f8..b62e58b27 100644 --- a/std/examples/curl.ts +++ b/std/examples/curl.ts @@ -1,4 +1,4 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. -const url = Deno.args[1]; -const res = await fetch(url); +const url_ = Deno.args[1]; +const res = await fetch(url_); await Deno.copy(Deno.stdout, res.body); diff --git a/std/examples/echo_server.ts b/std/examples/echo_server.ts index 3bed1bca2..1eafae50c 100644 --- a/std/examples/echo_server.ts +++ b/std/examples/echo_server.ts @@ -3,7 +3,6 @@ const hostname = "0.0.0.0"; const port = 8080; const listener = Deno.listen({ hostname, port }); console.log(`Listening on ${hostname}:${port}`); -while (true) { - const conn = await listener.accept(); +for await (const conn of listener) { Deno.copy(conn, conn); } diff --git a/std/examples/gist.ts b/std/examples/gist.ts index 890d85099..d0e369c66 100755 --- a/std/examples/gist.ts +++ b/std/examples/gist.ts @@ -1,7 +1,5 @@ #!/usr/bin/env -S deno --allow-net --allow-env // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. - -const { args, env, exit, readFile } = Deno; import { parse } from "https://deno.land/std/flags/mod.ts"; function pathBase(p: string): string { @@ -9,57 +7,53 @@ function pathBase(p: string): string { return parts[parts.length - 1]; } -async function main(): Promise<void> { - const token = env()["GIST_TOKEN"]; - if (!token) { - console.error("GIST_TOKEN environmental variable not set."); - console.error("Get a token here: https://github.com/settings/tokens"); - exit(1); - } - - const parsedArgs = parse(args.slice(1)); - - if (parsedArgs._.length === 0) { - console.error( - "Usage: gist.ts --allow-env --allow-net [-t|--title Example] some_file " + - "[next_file]" - ); - exit(1); - } - - const files = {}; - for (const filename of parsedArgs._) { - const base = pathBase(filename); - const content = await readFile(filename); - const contentStr = new TextDecoder().decode(content); - files[base] = { content: contentStr }; - } +const token = Deno.env()["GIST_TOKEN"]; +if (!token) { + console.error("GIST_TOKEN environmental variable not set."); + console.error("Get a token here: https://github.com/settings/tokens"); + Deno.exit(1); +} - const content = { - description: parsedArgs.title || parsedArgs.t || "Example", - public: false, - files: files - }; - const body = JSON.stringify(content); +const parsedArgs = parse(Deno.args.slice(1)); - const res = await fetch("https://api.github.com/gists", { - method: "POST", - headers: [ - ["Content-Type", "application/json"], - ["User-Agent", "Deno-Gist"], - ["Authorization", `token ${token}`] - ], - body - }); +if (parsedArgs._.length === 0) { + console.error( + "Usage: gist.ts --allow-env --allow-net [-t|--title Example] some_file " + + "[next_file]" + ); + Deno.exit(1); +} - if (res.ok) { - const resObj = await res.json(); - console.log("Success"); - console.log(resObj["html_url"]); - } else { - const err = await res.text(); - console.error("Failure to POST", err); - } +const files = {}; +for (const filename of parsedArgs._) { + const base = pathBase(filename); + const content = await Deno.readFile(filename); + const contentStr = new TextDecoder().decode(content); + files[base] = { content: contentStr }; } -main(); +const content = { + description: parsedArgs.title || parsedArgs.t || "Example", + public: false, + files: files +}; +const body = JSON.stringify(content); + +const res = await fetch("https://api.github.com/gists", { + method: "POST", + headers: [ + ["Content-Type", "application/json"], + ["User-Agent", "Deno-Gist"], + ["Authorization", `token ${token}`] + ], + body +}); + +if (res.ok) { + const resObj = await res.json(); + console.log("Success"); + console.log(resObj["html_url"]); +} else { + const err = await res.text(); + console.error("Failure to POST", err); +} |