Skip to content

A games launcher for GOG, Amazon and Epic Games for Linux, Windows and macOS.

600 B
Raw Blame
/**
 * Resolves with the result of `promise`, or with `fallback` if it takes longer
 * than `ms`. The original promise keeps running in the background, so any work
 * it does (like caching its result) is not lost.
 */
export function withTimeout<T>(
  promise: Promise<T>,
  ms: number,
  fallback: T
): Promise<T> {
  return new Promise<T>((resolve) => {
    const timer = setTimeout(() => resolve(fallback), ms)
    promise.then(
      (value) => {
        clearTimeout(timer)
        resolve(value)
      },
      () => {
        clearTimeout(timer)
        resolve(fallback)
      }
    )
  })
}