parseThenable
Parses a value into a thenable (Promise-like) object. This parser combines the validateThenable protocol validator with the normalizeThenable normalizer to ensure that the input is an object or function with a callable then property.
The parser never throws, never mutates input, and never invokes the then method. It returns a structured ParseResult<PromiseLike<T>> describing success or failure.
Signature
function parseThenable<T = unknown>(
value: unknown
): ParseResult<PromiseLike<T>>
Behavior
- Accepts:
- Native Promises
- Custom thenables
- Objects or functions with a callable then property
- Rejects:
- Primitives.
- null and undefined.
- Objects without a callable then.
- Functions without a then property.
- Never invokes then.
- Never wraps values or creates Promises.
- Never throws and never mutates input.
Returns
ok: trueAnd the original thenable when parsing succeeds.ok: false, value: null: And a list of issues when parsing fails.
Examples
parseThenable(Promise.resolve(1))
// { ok: true, value: Promise(1), issues: [] }
parseThenable({ then: () => {} })
// { ok: true, value: { then: [Function] }, issues: [] }
parseThenable(123)
// { ok: false, value: null, issues: ["Value is not a thenable (Promise-like) object"] }
Notes
- This parser is ideal for safely handling Promise-like values without triggering execution.
- Useful for async pipelines, lazy evaluation, and interoperability with libraries that implement custom thenables.