Last active
June 3, 2020 21:08
-
-
Save cevaris/3b43212956bb8b51b61fccf71e5d8f0f to your computer and use it in GitHub Desktop.
Using Promise.race and setTimeout for timeout logic for Promise execution
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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