Appearance
JavaScript
Core JavaScript language patterns — runtime-agnostic, applies equally to browser and Node.js code.
Async
- Async JavaScript Patterns: Promise combinators including
allSettled, race timeouts, retry with backoff, and concurrency-limited mapping. Use when writing async control flow. - Async Pipe: Compose async functions into a sequential pipeline where each step receives the previous output. Use when chaining multiple async transformations.
- Deferred Promise: A promise whose resolve/reject are accessible from outside the executor. Use when the creator and settler of a promise are in different scopes.
- Delay and Interval Utilities: Promise-based delays, random stagger for animations, and frame-synced intervals. Use when you need timing utilities in async code.
- Non-Concurrent Operations:
Lockclass andnonConcurrentwrapper that serialize async operations. Use when only one instance of an operation should run at a time. - Progress Tracking:
EventEmitter-based progress reporting for batch async operations. Use when showing progress feedback for uploads, API calls, or parallel tasks. - Resolve All Promises: Prevent event loop hangs from unresolved promises in background async work. Use when running fire-and-forget async functions, background loops, or async iterator consumers.
- Timer Patterns: Cancellable, awaitable timers with explicit state management and elapsed-time measurement. Use when scheduling one-shot actions or measuring durations.
Async Iterators
- Async Iterators with Repeater: Repeater library fundamentals —
push/stop, backpressure,SlidingBuffer,merge, andlatestcombinators. Use when building async iterables. - Chunking Async Iterables: Fixed-size batching for arrays and async streams. Use when processing data in bulk.
- Debouncing Async Iterables: Collapse rapid async events into single values after a quiet period. Use when coalescing bursts from event sources.
- Event to Async Iterator: Convert
EventEmitterorEventTargetsources into async iterables with automatic cleanup. Use when bridging events tofor await...of. - Multicasting Async Iterables: Fan out values from one async iterable to multiple subscribers. Use when multiple consumers need the same stream.
Collections
- Dot-Path Property Access: Tagged template literal that safely traverses deeply nested properties like
user.profile.address.city. Use when reading nested object paths known at definition time. - Filter Object: Create a new object containing only allow-listed keys. Use when sanitizing user input or stripping unexpected properties.
- Options with Defaults: Merge user options and default options using spread or nullish coalescing. Use when a function accepts optional configuration.
Modules
- Index File Re-exports: Barrel files that re-export directory modules through a single entry point, converting defaults to named exports. Use when organizing module imports into clean paths.
Patterns
- Automatic Resource Management: Create-use-dispose lifecycle that guarantees resource cleanup even when errors occur. Use when managing temp dirs, connections, or file handles.
- Composable Schema Validation: Chainable validators with message overrides, nesting, and value coercion. Use when validating user input, form fields, or API payloads.
- Enum Classes: Sealed, frozen enum instances with identity semantics and iteration. Use when you need a fixed set of named values that prevent invalid states.
- Layered Dispatch: First-match routing that dispatches items to handlers based on predicate tests, like Express middleware. Use when processing heterogeneous collections where each item needs different treatment.
- Readable Regular Expressions: Tagged template literals that build regex from named, composable pieces. Use when a regex is too complex to read as a single literal.
- RelativeURL: URL subclass that works with relative paths without requiring an origin. Use in SPAs or server-side route generation where the origin is irrelevant.
- Result Type: Frozen
Resultclass withok/errfactories and atry()wrapper that makes error handling explicit instead of throwing. Use for parsing, validation, or I/O where failure is expected. - Class-Based State Machines:
EventEmitter-based state machine with lifecycle hooks and defined transitions. Use when a workflow has explicit states and boolean flags become unmanageable.
Random Utilities
- Random Utilities: Helper functions for random floats in a range, random integers, Fisher-Yates shuffle, and picking random elements. Use when you need randomness utilities beyond raw Math.random().
Telemetry
- Unit-of-Work Telemetry: Accumulate context throughout a unit of work and emit one rich log entry when it completes. Extends to function-level tracing via
AsyncLocalStorage. Use when implementing structured logging or tracing. - Custom Logger: Pino-based logger factory with auto-detected module names, Google Cloud severity mapping, and child loggers. Use when setting up structured logging for an application.
- Browser Log Forwarding: Forward browser-side log entries and uncaught errors to a server endpoint so client and server logs appear in one stream. Use when building full-stack apps.
See also
- Express.js Request Logging: Express middleware that produces one structured log entry per request using canonical log lines.