aboutsummaryrefslogtreecommitdiff
path: root/docs/api/console.md
diff options
context:
space:
mode:
authorGravatar Colin McDonnell <colinmcd94@gmail.com> 2023-02-23 17:13:30 -0800
committerGravatar GitHub <noreply@github.com> 2023-02-23 17:13:30 -0800
commitf54300578b1edc7f67daddbfae29575cbf305264 (patch)
tree1437f3274122c011f879dca71f59a74d75a33fd0 /docs/api/console.md
parent5929daeeae1f528abab31979a0a28bc87a03b1f4 (diff)
downloadbun-f54300578b1edc7f67daddbfae29575cbf305264.tar.gz
bun-f54300578b1edc7f67daddbfae29575cbf305264.tar.zst
bun-f54300578b1edc7f67daddbfae29575cbf305264.zip
Add documentation (#2148)bun-v0.5.7
* Add documentation * Tweaks * Fixes * Rearrange * Update
Diffstat (limited to 'docs/api/console.md')
-rw-r--r--docs/api/console.md38
1 files changed, 38 insertions, 0 deletions
diff --git a/docs/api/console.md b/docs/api/console.md
new file mode 100644
index 000000000..49b8a1b79
--- /dev/null
+++ b/docs/api/console.md
@@ -0,0 +1,38 @@
+{% callout %}
+**Note** — Bun provides a browser- and Node.js-compatible [console](https://developer.mozilla.org/en-US/docs/Web/API/console) global. This page only documents Bun-native APIs.
+{% /callout %}
+
+In Bun, the `console` object can be used as an `AsyncIterable` to sequentially read lines from `process.stdin`.
+
+```ts
+for await (const line of console) {
+ console.log(line);
+}
+```
+
+This is useful for implementing interactive programs, like the following addition calculator.
+
+```ts#adder.ts
+console.log(`Let's add some numbers!`);
+console.write(`Count: 0\n> `);
+
+let count = 0;
+for await (const line of console) {
+ count += Number(line);
+ console.write(`Count: ${count}\n> `);
+}
+```
+
+To run the file:
+
+```bash
+$ bun adder.ts
+Let's add some numbers!
+Count: 0
+> 5
+Count: 5
+> 5
+Count: 10
+> 5
+Count: 15
+```