Created
November 23, 2018 05:45
-
-
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
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
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