Skip to content

Instantly share code, notes, and snippets.

@squalvj
Created November 23, 2018 05:45
Show Gist options
  • Save squalvj/0ccfd30328141bf7711e949a54a249bf to your computer and use it in GitHub Desktop.
Save squalvj/0ccfd30328141bf7711e949a54a249bf to your computer and use it in GitHub Desktop.
Show and Hide loading using seperate promise file and using try and catch
function myPromise() {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve('Stack Overflow');
// reject('Some Error')
}, 2000);
});
}
function showSpinner() {
document.getElementById('loader').style.display = 'block';
}
function hideSpinner() {
document.getElementById('loader').style.display = 'none';
}
async function sayHello() {
try {
showSpinner()
const externalFetchedText = await myPromise();
console.log('Hello ' + externalFetchedText);
} catch (err) {
console.error(err);
} finally {
hideSpinner()
}
}
sayHello();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment