Skip to content

Instantly share code, notes, and snippets.

@graup
Created November 26, 2018 05:02
Show Gist options
  • Save graup/8c871fe64171c49efea05650cc80a7bc to your computer and use it in GitHub Desktop.
Save graup/8c871fe64171c49efea05650cc80a7bc to your computer and use it in GitHub Desktop.
const SLOW_LOAD_TIME = 1500;
function waitOrLoad(callbackIfSlow, maximumLoadTime = SLOW_LOAD_TIME) {
const timeout = setTimeout(callbackIfSlow, maximumLoadTime);
return function loadingFinished() {
clearTimeout(timeout);
}
}
async function doSomething() {
const loadingFinished = waitOrLoad(() => {
console.log('loading is slow');
});
// do something that the user expects to take some time, e.g. loading data ...
loadingFinished();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment