validateThenable
Checks whether a value is a thenable (Promise‑like) object. This helper uses the isThenable type guard internally. A value is considered thenable when it is an object or function with a callable then method that follows the structural shape of a Promise.
This validator does not attempt to unwrap, resolve, or coerce the value. It only verifies the presence of a valid then method. It never throws and never mutates input. Use it when you need to ensure a value behaves like a Promise before chaining or awaiting it.
Signature
function validateThenable<T = unknown>(
value: unknown
): PromiseLike<T> | undefined
Parameters
| Name | Type | Description |
|---|---|---|
| value | unknown |
The value to validate. |
| field | string |
The name of the field being validated, used in error reporting. |
Returns
One of:
- The original input if the value is a thenable.
undefined: If the value is not a thenable.
Behavior
- Uses isThenable internally.
- Accepts:
- Real Promises.
- Objects whose then method is structurally compatible with
PromiseLike. - Functions that implement a valid then method.
- Rejects:
- Primitives.
nullandundefined.- Objects with non‑function
thenproperties. - Functions without a
thenmethod. - Never throws and never mutates input.
- Never coerces values.
Examples
validateThenable(Promise.resolve(123))
// Promise.resolve(123)
validateThenable({ then: Promise.resolve(1).then.bind(Promise.resolve(1)) })
// { then: ... }
validateThenable(123)
// undefined
validateThenable({ then: "not-a-function" })
// undefined
Notes
- This validator does not resolve or execute the thenable; it only checks shape.
- It does not guarantee full
Promisesemantics beyond the presence of a callable then method. - Suitable for APIs that accept
Promise‑like values, lazy evaluators, or user‑provided async handlers. - The validator is pure, side‑effect‑free, and never throws.