Last active
December 22, 2018 21:33
-
-
Save stephenjwatkins/a46aa642b8cf1963fcaaa99f2fbc5299 to your computer and use it in GitHub Desktop.
Simple faux loader that runs a promise for a minimum time.
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
| // This function will return a promise that runs the passed in promise, but | |
| // for at least the time specified. This is helpful for UI interactions | |
| // that should look like they're processing something. | |
| // | |
| // By design, if an error occurs, it'll still wait for the time specified | |
| // before throwing. | |
| async function runPromiseForMinimumTime(promise, minimumTime = 500) { | |
| let error; | |
| const [, result] = await Promise.all([ | |
| new Promise(r => setTimeout(r, minimumTime)), | |
| promise.catch(e => { | |
| error = e; | |
| }), | |
| ]); | |
| if (error) { | |
| throw error; | |
| } | |
| return result; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment