// Result-or-Promise helpers export type RP = T | Promise; // Returns true when v is a Promise or a has a then function. export function isPromise(v: RP): v is Promise { return ( v !== null && (typeof v === "object" || typeof v === "function") && typeof (v as any).then === "function" ); } export function rpThen(v: RP, f: (a: A) => RP): RP { return isPromise(v) ? (v as Promise).then(f) : f(v as A); } /** * Collect an array of Result-or-Promise values into a single array * avoiding Promise allocation when all inputs are synchronous. */ export function rpAll(arr: RP[]): RP { for (let i = 0; i < arr.length; i++) { if (isPromise(arr[i])) { return Promise.all(arr as Promise[]); } } return arr as T[]; }