Overview
The “tee” methods allow you to execute side effects (like logging or metrics) while passing the original Result through unchanged. Errors in the side effect function are caught and ignored, ensuring your main logic continues unaffected.andTee() - Ok Path Side Effects
Signature
Parameters
(t: T) => unknown
required
A side effect function that receives the Ok value. The return value is ignored. Only called if the Result is Ok.
Returns
Returns the originalResult<T, E> unchanged:
- If the Result is
Ok(value), executesf(value)and returns the originalOk(value) - If the Result is
Err(error), returnsErr(error)without callingf - Errors thrown by
fare caught and ignored
orTee() - Err Path Side Effects
Signature
Parameters
(e: E) => unknown
required
A side effect function that receives the Err value. The return value is ignored. Only called if the Result is Err.
Returns
Returns the originalResult<T, E> unchanged:
- If the Result is
Err(error), executesf(error)and returns the originalErr(error) - If the Result is
Ok(value), returnsOk(value)without callingf - Errors thrown by
fare caught and ignored
Examples
Basic andTee Usage
Basic orTee Usage
Logging Pipeline
Error Logging
Metrics Collection
Resilient Side Effects
Multiple Tees in Pipeline
Debug Logging
Audit Trail
Implementation Details
andTee for Ok (result.ts:348-355)
andTee for Err (result.ts:443-445)
orTee for Ok (result.ts:357-359)
orTee for Err (result.ts:447-454)
Key Characteristics
- Original value preserved: The Result passes through unchanged
- Errors are swallowed: Exceptions in the side effect are caught and ignored
- Return value ignored: Whatever the function returns is discarded
- No type changes: Error types don’t accumulate from side effects
- Safe for logging: Perfect for logging, metrics, debugging
Differences from andThrough/asyncAndThrough
Unlike andThrough(), the “tee” methods:- Ignore the return value of the side effect function
- Catch and ignore exceptions
- Don’t add error types to the Result
- Are meant purely for side effects that should never fail the main computation
Use Cases
- Logging: Debug or production logging without affecting the pipeline
- Metrics: Collect analytics and performance metrics
- Debugging: Inspect values during development
- Audit trails: Record operations without coupling to audit system failures
- Notifications: Send alerts without blocking main logic
- Caching: Populate caches as a side effect
Related
- Result.andThrough() - Side effects that can fail
- Result.map() - Transform Ok values
- Result.andThen() - Chain operations