Skip to content

Instantly share code, notes, and snippets.

@cevaris
Last active June 3, 2020 21:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cevaris/3b43212956bb8b51b61fccf71e5d8f0f to your computer and use it in GitHub Desktop.
Save cevaris/3b43212956bb8b51b61fccf71e5d8f0f to your computer and use it in GitHub Desktop.
Using Promise.race and setTimeout for timeout logic for Promise execution
export const REPORT_TIMEOUT_MS = 5000;
const TIMEOUT_ERROR = new Error('Ran out of time!!');
const TIMEOUT_PROMISE = new Promise<boolean>(
(_, reject) => setTimeout(() => reject(TIMEOUT_ERROR), REPORT_TIMEOUT_MS)
);
const myPromise = new Promise(...);
const success: boolean = await Promise.race([myPromise, TIMEOUT_PROMISE]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment