wulfeyW
Denoβ€’3y agoβ€’
11 replies
wulfey

Preventing `Deno.stdin` reads from blocking

In a module for reading Deno.stdin input, there is a loop that reads and parses the returned bytes.
My issue is that, in case of an uncaught error/rejection, the process hangs until stdin has something to return, and once it does, only then does it exit the process and log the error information.
Is there a way to make this read() non-blocking in that sense?
If the user does not input anything, but an error occurs, the process should exit immediately and not have to wait for a keypress.
The code below can be run from a file for demonstration.
// Uncaught error timeout
setTimeout(() => { throw new Error(); }, 5000);

const input = Deno.stdin;
input.setRaw(true, Deno.build.os === "windows" ? undefined : { cbreak: true });
while (true) {
  let bytes: Uint8Array = new Uint8Array(4096);
  // If an uncaught error occurs somewhere else
  // Deno waits for the read to complete before exiting.
  await input.read(bytes);
  console.log("read some bytes");
  /* ... */
}
Was this page helpful?