Replies: 3 comments 6 replies
-
const child = new Deno.Command('command').spawn()
const writer = child.stdin.getWriter()
await writer.write(new TextEncoder().encode("potato\n"))
writer.releaseLock()
const [stdout, stderr] = await Promise.all([
collect(child.stdout),
collect(child.stderr)
])
async function collect(readable: ReadableStream<Uint8Array>): Uint8Array {
return Uint8Array.from((await Array.fromAsync(readable)).map(x => [...x]).flat())
} |
Beta Was this translation helpful? Give feedback.
-
#14297 proposes to add an |
Beta Was this translation helpful? Give feedback.
-
If you want your JavaScript code to do literally anything while the Deno command is executing you can't use an event blocking function like outputSync. JavaScript is single threaded. You need to utilise promises so JavaScript can switch between tasks of getting the output of the Deno command, pushing input in and doing literally anything else while it waits. |
Beta Was this translation helpful? Give feedback.
-
The docs for outputSync() say Will throw an error if stdin: "piped" is set.
So if I want to run a program and pass it some stuff on stdin am I doomed to use the async interface, and thus make my wrapper for it async, and in turn to have to remember to await whateverWrapperFunc() every time I want to use it? I really hate that because sooner or later I'm going to forget to await and get a race.
While I'm ranting about this if javascript want to add syntax support for promises why not go the whole hog and also add 'default sync callers' or something so we don't have to have double interfaces to everything or litter client code with await everywhere and get a race if we forget god
Beta Was this translation helpful? Give feedback.
All reactions