summaryrefslogtreecommitdiff
path: root/std/examples
diff options
context:
space:
mode:
authorAndy Hayden <andyhayden1@gmail.com>2019-10-27 06:04:42 -0700
committerRy Dahl <ry@tinyclouds.org>2019-10-27 09:04:42 -0400
commitaec5a646c9218a0a0da62cbcd1f28bc23c242540 (patch)
treee052f0263eb2abc4915dc3710ec44ee34e9ad621 /std/examples
parent51dd91a3ccfd9554bcf69b539f2f748da81c5b12 (diff)
feat: top-level-for-await (#3212)
Diffstat (limited to 'std/examples')
-rw-r--r--std/examples/cat.ts13
-rw-r--r--std/examples/catj.ts46
-rw-r--r--std/examples/curl.ts10
3 files changed, 29 insertions, 40 deletions
diff --git a/std/examples/cat.ts b/std/examples/cat.ts
index 3626e3c29..9e713d862 100644
--- a/std/examples/cat.ts
+++ b/std/examples/cat.ts
@@ -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));
diff --git a/std/examples/catj.ts b/std/examples/catj.ts
index cbe06d4b8..87fd6a964 100644
--- a/std/examples/catj.ts
+++ b/std/examples/catj.ts
@@ -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();
diff --git a/std/examples/curl.ts b/std/examples/curl.ts
index 04dd9d601..e020016f8 100644
--- a/std/examples/curl.ts
+++ b/std/examples/curl.ts
@@ -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);