Created
May 14, 2020 04:54
-
-
Save PH4N70M-0P5/3c01436499d308727fea8cbac6a40c2f to your computer and use it in GitHub Desktop.
async function 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
//first we define our new function as async and give it a name of f, with no arguments... or data between () | |
async function f() { | |
//we make some data that will run normally | |
let result = 'first!'; | |
//we make a new promise that takes 2 arguments, resolve, reject | |
let promise = new Promise((resolve, reject) => { | |
//we set a timeout so that out promise takes 1 second before it is finished | |
//when it is done, we resolve it with the messsage done! | |
setTimeout(() => resolve('done!'), 1000); | |
}); | |
//we tell result to WAIT for promise to complete before we run result | |
result = await promise; | |
//QUESTION what will print first? first! or done!?? | |
console.log(result); | |
} | |
//TO RUN THE CODE | |
//uncomment the line below | |
//f() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment