Skip to content

Instantly share code, notes, and snippets.

@stephenjwatkins
Last active December 22, 2018 21:33
Show Gist options
  • Save stephenjwatkins/a46aa642b8cf1963fcaaa99f2fbc5299 to your computer and use it in GitHub Desktop.
Save stephenjwatkins/a46aa642b8cf1963fcaaa99f2fbc5299 to your computer and use it in GitHub Desktop.
Simple faux loader that runs a promise for a minimum time.
// 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