Created
July 3, 2017 09:09
-
-
Save romanonthego/104bc1f2aab0e2748df18672059dfaff to your computer and use it in GitHub Desktop.
This file contains 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 function promiseWithTimeout(promise, timeoutTime = 5000) { | |
return new Promise((resolve, reject) => { | |
const timeout = setTimeout(() => reject({message: 'timeout'}), timeoutTime) | |
const resolveWithTimeout = (res) => { | |
clearTimeout(timeout) | |
resolve(res) | |
} | |
const rejectWithTimeout = (err) => { | |
clearTimeout(timeout) | |
reject(err) | |
} | |
promise.then(resolveWithTimeout).catch(rejectWithTimeout) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment