Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Resource Management

Avoid leaks and manage resources properly.

The Golden Rule

Always consume process output.

import { run } from "jsr:@j50n/proc@0.23.3";

// ❌ Resource leak
const p = run("ls");
// Output never consumed!

// ✅ Output consumed
await run("ls").lines.collect();

Why This Matters

Unconsumed output keeps the process handle open, preventing cleanup.

Ways to Consume Output

collect()

const lines = await run("ls").lines.collect();

forEach()

await run("ls").lines.forEach(line => {
  console.log(line);
});

for-await

for await (const line of run("ls").lines) {
  console.log(line);
}

toStdout()

await run("ls").toStdout();

Aggregations

const count = await run("ls").lines.count();
const first = await run("ls").lines.first;

Checking Status

Consume output before checking status:

const p = run("command");
await p.lines.collect();  // Consume first
const status = await p.status;  // Then check

Error Handling

Errors automatically clean up resources:

try {
  await run("false").lines.collect();
} catch (error) {
  // Resources cleaned up automatically
}

Long-Running Processes

For processes that run indefinitely:

// This is fine - consuming output as it arrives
for await (const line of run("tail", "-f", "log").lines) {
  process(line);
}

Best Practices

  1. Always consume output - Use collect(), forEach(), or iterate
  2. Check status after consuming - Don't check status first
  3. Let errors propagate - They clean up automatically
  4. Use try-finally for cleanup - If you need custom cleanup

Next Steps