aboutsummaryrefslogtreecommitdiff
path: root/docs/api/console.md
blob: 49b8a1b79722e59328c022da46bb2d3c04bf5c6f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
```
n title='2022-12-03 23:57:13 -0800'>2022-12-03[test] Add a couple tests for subarray toEqualGravatar Jarred Sumner 1-0/+3 2022-12-03[fetch] Fix bug where .arrayBuffer() on an empty Response body returned a `Ui...Gravatar Jarred Sumner 1-1/+1 2022-12-03Don't invalidate previous file descriptro to avoid tripping assertionGravatar Jarred Sumner 1-5/+0 2022-12-03miscGravatar Jarred Sumner 3-1/+31 2022-12-03Add missing typeGravatar Jarred Sumner 1-0/+5 2022-12-03`process.stdout` and `process.stderr`Gravatar Jarred Sumner 15-564/+1537 2022-12-03simdutf ascii validation is about 20% faster on arm64 than our zig simd @Vect...Gravatar Jarred Sumner 1-0/+3 2022-12-03typo in readme (#1576)Gravatar Reed Jones 1-2/+2