Skip to main content

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 original Result<T, E> unchanged:
  • If the Result is Ok(value), executes f(value) and returns the original Ok(value)
  • If the Result is Err(error), returns Err(error) without calling f
  • Errors thrown by f are 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 original Result<T, E> unchanged:
  • If the Result is Err(error), executes f(error) and returns the original Err(error)
  • If the Result is Ok(value), returns Ok(value) without calling f
  • Errors thrown by f are 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

  1. Original value preserved: The Result passes through unchanged
  2. Errors are swallowed: Exceptions in the side effect are caught and ignored
  3. Return value ignored: Whatever the function returns is discarded
  4. No type changes: Error types don’t accumulate from side effects
  5. 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