2

I'd like to the read the output of an async spawn in real time with bun

import { spawn } from "bun";
const { stdout } = spawn("some command");

// doesn't work with bun (works with node.js)
stdout.on('data', (chunk) => {
    ...
})

In the example shown on the bun website, you have to wait for the process to complete before getting the output: https://github.com/oven-sh/bun#bunspawn--spawn-a-process

Laurent
  • 21
  • 2

1 Answers1

1

You can for await Subprocess.stdout to read chunks from the ReadableStream asynchronously.

const proc = Bun.spawn({
    cmd: ["some", "command"]
});

for await (const chunk of proc.stdout) {
    console.log(new TextDecoder().decode(chunk));
}

(TextDecoder is used here because chunk is a Uint8Array)

pr -
  • 240
  • 2
  • 9