DenoDDeno
Powered by
mattvrM
Denoβ€’3y ago
mattvr

How to re-open Deno.stdin?

I'm noticing that Deno gives an error when trying to
window.prompt
window.prompt
after I've consumed
stdin
stdin
.

// prompt.ts
// echo "test" | deno run prompt.ts

let message = ''
const decoder = new TextDecoder();

for await (const chunk of Deno.stdin.readable) {
  const textChunk = decoder.decode(chunk);
  message += textChunk
}

console.log('got', message)

window.prompt('hello?')
// prompt.ts
// echo "test" | deno run prompt.ts

let message = ''
const decoder = new TextDecoder();

for await (const chunk of Deno.stdin.readable) {
  const textChunk = decoder.decode(chunk);
  message += textChunk
}

console.log('got', message)

window.prompt('hello?')


When invoked:
> echo "test" | deno run prompt.ts
got test

error: Uncaught (in promise) BadResource: Bad resource ID
window.prompt('hello?')
       ^
    at isatty (ext:runtime/40_tty.js:19:7)
    at prompt (ext:runtime/41_prompt.js:36:8)
    at [..]/prompt.ts:14:8
> echo "test" | deno run prompt.ts
got test

error: Uncaught (in promise) BadResource: Bad resource ID
window.prompt('hello?')
       ^
    at isatty (ext:runtime/40_tty.js:19:7)
    at prompt (ext:runtime/41_prompt.js:36:8)
    at [..]/prompt.ts:14:8


Is it not possible to re-open
Deno.stdin
Deno.stdin
for reading more than once? I notice it's closed from
Deno.resources
Deno.resources
after the initial read.

I'm doing this for a command line utility which supports I/O piping. Thanks for any help!
Deno banner
DenoJoin
Chat about Deno, a modern runtime for JavaScript and TypeScript.
20,944Members
Resources
Recent Announcements

Similar Threads

Was this page helpful?

Similar Threads

Deno.stdin raw map?
DNADDNA / help
2y ago
deno command stdin, stdout continous reading
TangJieHaoTTangJieHao / help
3y ago
Preventing `Deno.stdin` reads from blocking
vukmeanswolfVvukmeanswolf / help
3y ago
Synchronous read single line from Deno.stdin
Octal!OOctal! / help
4y ago