Skip to content

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 value is a function, it is called and its return value (sync or async) is awaited. Synchronous throws are converted to rejections.
  • Otherwise, value is 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); // → 42
ts
// Sync function
await toPromise(() => computeResult()); // → result
ts
// Async function
await toPromise(() => fetch('/api/data').then(r => r.json()));