File size: 1,178 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
export type LazyResult<TValue> = PromiseLike<TValue> & { value?: TValue }
export type ResolvedLazyResult<TValue> = PromiseLike<TValue> & { 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<TValue>(
  fn: () => Promise<TValue>
): LazyResult<TValue> {
  let pendingResult: Promise<TValue> | undefined

  const result: LazyResult<TValue> = {
    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<TValue>(
  result: LazyResult<TValue>
): result is ResolvedLazyResult<TValue> {
  return result.hasOwnProperty('value')
}