Task Queue#
Background job processing with real cancellation and easy testing.
The Problem#
Background jobs need retries, timeouts, and error handling. In vanilla TypeScript:
- Promise.race doesn't actually cancel the losing promise
- Dependencies are hardcoded, making tests awkward
- Error context gets lost after retries
Run Both Versions#
bun run examples/task-queue/without-purus.ts
bun run examples/task-queue/with-purus.ts
Without purus#
See without-purus.ts:
- Promise.race for timeout (but the job keeps running!)
- Hardcoded logger - can't mock without a DI framework
- Errors are
unknownafter retry exhaustion
With purus#
See with-purus.ts:
- timeout() returns cleanup function - job actually stops
- provide(env) injects dependencies - swap for tests
- Typed errors preserved through the pipeline
Key Takeaways#
- Return a cleanup function from async() for real cancellation
- provide() makes testing easy without frameworks
- catchAll() preserves error context