Skip to content

Instantly share code, notes, and snippets.

@techtocore
Last active May 12, 2020 12:06
Show Gist options
  • Save techtocore/9a877b11d3c9324d14bcc89e8f852876 to your computer and use it in GitHub Desktop.
Save techtocore/9a877b11d3c9324d14bcc89e8f852876 to your computer and use it in GitHub Desktop.
async-await Example
function slowFunction() {
return new Promise((resolve, reject) => {
// This is where your API call would happen. Wherever you are able to access the required value, call resolve(VALUE)
setTimeout(() => {
resolve({
'key': 'ans'
});
}, 2000);
})
}
async function main() {
let ans = {
'key': 'val'
};
ans = await slowFunction();
console.log(ans); // Note that the previously set value should not be displayed dispite having a 'slow' function in between.
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment