Skip to content

Overview

Protocol safe parsers normalize values based on the protocols they implement, such as iteration or async iteration. These parsers check for callable protocol methods like [Symbol.iterator] or [Symbol.asyncIterator] and return structured ParseResult<T> objects.

Protocol parsers never throw, never mutate input, and never coerce values. They ensure that downstream logic can rely on the presence of required protocol methods.

Available protocol safe parsers

  • parseAsyncIterable: Ensures a value implements [Symbol.asyncIterator].
  • parseCallable: Ensures a value is a callable function with a valid call signature.
  • parseIterable: Ensures a value implements [Symbol.iterator].
  • parseThenable: Ensures a value implements a callable .then() method and behaves like a Promise.

Each parser is strict and focused on protocol correctness.

When to use protocol safe parsers

Use protocol parsers when:

  • You need to confirm that a value supports iteration or async iteration.
  • You want to avoid runtime errors from missing protocol methods.
  • You want predictable behavior before consuming a value in loops or streams.
  • You want structured results instead of boolean checks.
  • You want workflows that are explicit, defensive, and easy to reason about.

Protocol parsers are ideal when working with streams, generators, or any logic that depends on iteration semantics.

Why protocol safe parsers matter

Protocol parsers give you:

  • Guaranteed protocol support
    No more “value is not iterable” runtime errors.

  • Consistent return shapes
    Every parser returns { ok, value, issues }.

  • Strict, documented behavior
    Only values with callable protocol methods are accepted.

  • Zero side effects
    No mutation, no throwing, no global state.

Protocol safe parsers help you write robust, predictable iteration logic in both sync and async workflows.