Last active
May 12, 2020 12:06
-
-
Save techtocore/9a877b11d3c9324d14bcc89e8f852876 to your computer and use it in GitHub Desktop.
async-await Example
This file contains 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 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