Skip to content

Instantly share code, notes, and snippets.

@sam-ngu
Last active April 3, 2020 09:57
Show Gist options
  • Save sam-ngu/92d47b9d1b76a710e0481cc507c2159c to your computer and use it in GitHub Desktop.
Save sam-ngu/92d47b9d1b76a710e0481cc507c2159c to your computer and use it in GitHub Desktop.
Async Demo
// adding the 'async' keyword in front of a function will make it asynchronous.
// JS will automatically turn the output into a Promise
async function sayHello(){
return 'Hello"
}
sayHello().then((response) => {
console.log(response) // expected output: 'Hello'
})
// Using 'await' will get the response of a Promise directly without using the then function.
// But this will execute the Promise synchronously, ie it will wait the promise to finish
// before moving on to the next line.
let response = await sayHello();
console.log(response); // expected output: 'Hello'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment