Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save PH4N70M-0P5/3c01436499d308727fea8cbac6a40c2f to your computer and use it in GitHub Desktop.
Save PH4N70M-0P5/3c01436499d308727fea8cbac6a40c2f to your computer and use it in GitHub Desktop.
async function example
//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