Skip to content

Commit 4979581

Browse files
bartlomiejuclaude
andcommitted
docs: add Deno.spawn() convenience APIs to subprocess tutorial
Document the new unstable Deno.spawn(), Deno.spawnAndWait(), and Deno.spawnAndWaitSync() shorthand functions added in Deno 2.7. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 3bc9f66 commit 4979581

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

examples/tutorials/subprocess.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,48 @@ const status = await process.status;
133133
console.log("Exit code:", status.code);
134134
```
135135

136+
## Convenience spawn functions
137+
138+
:::caution
139+
140+
`Deno.spawn()`, `Deno.spawnAndWait()`, and `Deno.spawnAndWaitSync()` are
141+
unstable APIs introduced in Deno 2.7. They may evolve before being stabilized.
142+
143+
:::
144+
145+
As of Deno 2.7, three shorthand functions provide a more concise alternative to
146+
`Deno.Command`:
147+
148+
- **`Deno.spawn(command, args, options?)`** — spawns a subprocess and returns a
149+
`ChildProcess` (equivalent to `new Deno.Command(cmd, { args }).spawn()`)
150+
- **`Deno.spawnAndWait(command, args, options?)`** — spawns a subprocess and
151+
returns a promise resolving to `CommandOutput` (equivalent to
152+
`new Deno.Command(cmd, { args }).output()`)
153+
- **`Deno.spawnAndWaitSync(command, args, options?)`** — synchronous variant
154+
that blocks until the subprocess completes
155+
156+
```ts title="spawn_examples.ts"
157+
// Spawn a child process
158+
const child = Deno.spawn("echo", ["hello"]);
159+
160+
// Spawn and wait for output
161+
const output = await Deno.spawnAndWait("git", ["status"]);
162+
console.log(new TextDecoder().decode(output.stdout));
163+
164+
// Spawn with options
165+
const formatted = Deno.spawn("deno", ["fmt", "--check"], {
166+
stdout: "inherit",
167+
});
168+
169+
// Synchronous execution
170+
const result = Deno.spawnAndWaitSync("echo", ["done"]);
171+
```
172+
173+
These functions accept the command and arguments as separate parameters, which
174+
can be more readable than the `Deno.Command` constructor options pattern.
175+
176+
## Convenience methods on output streams
177+
136178
Available convenience methods include:
137179

138180
- `.text()` - Returns the output as a UTF-8 string

0 commit comments

Comments
 (0)