export type LazyResult = PromiseLike & { value?: TValue } export type ResolvedLazyResult = PromiseLike & { value: TValue } /** * Calls the given async function only when the returned promise-like object is * awaited. Afterwards, it provides the resolved value synchronously as `value` * property. */ export function createLazyResult( fn: () => Promise ): LazyResult { let pendingResult: Promise | undefined const result: LazyResult = { then(onfulfilled, onrejected) { if (!pendingResult) { pendingResult = fn() } pendingResult .then((value) => { result.value = value }) .catch(() => { // The externally awaited result will be rejected via `onrejected`. We // don't need to handle it here. But we do want to avoid an unhandled // rejection. }) return pendingResult.then(onfulfilled, onrejected) }, } return result } export function isResolvedLazyResult( result: LazyResult ): result is ResolvedLazyResult { return result.hasOwnProperty('value') }