@wpkernel/pipeline / maybeThen
Function: maybeThen()
ts
function maybeThen<T, TResult>(value, onFulfilled): MaybePromise<TResult>;Maps a synchronous value or structurally valid thenable while preserving the synchronous path.
For synchronous input, onFulfilled runs before this function returns and its value is returned directly. Throws from that callback remain synchronous. For a thenable, the captured method is adopted exactly once into a native promise; callback throws then become promise rejections. A throwing then getter remains a synchronous throw.
Type Parameters
T
T
TResult
TResult
Parameters
value
MaybePromise<T>
Value or thenable to map.
onFulfilled
(value) => MaybePromise<TResult>
Transformation applied to the fulfilled value.
Returns
MaybePromise<TResult>
The callback result directly for synchronous input, or a native chained promise for thenable input.
Example
ts
const immediate = maybeThen(2, (value) => value * 3);
isPromiseLike(immediate); // false
const deferred = maybeThen(Promise.resolve(2), (value) => value * 3);
isPromiseLike(deferred); // true