Documentation / @andrew_l/toolkit / toPromise
Function: toPromise() ​
toPromise<
T>(value):Promise<ToPromiseResult<T>>
Wraps a value or a thunk in a Promise, always resolving on the next microtask.
- If
valueis a function, it is called and its return value (sync or async) is awaited. Synchronous throws are converted to rejections. - Otherwise,
valueis resolved as-is.
Type Parameters ​
T ​
T
Parameters ​
value ​
T
A plain value or a zero-argument function returning Awaitable<T>.
Returns ​
Promise<ToPromiseResult<T>>
A Promise that resolves to the value or the function's result.
Examples ​
ts
// Plain value
await toPromise(42); // → 42ts
// Sync function
await toPromise(() => computeResult()); // → resultts
// Async function
await toPromise(() => fetch('/api/data').then(r => r.json()));