Last active
April 3, 2020 09:57
-
-
Save sam-ngu/92d47b9d1b76a710e0481cc507c2159c to your computer and use it in GitHub Desktop.
Async Demo
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
// 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