Last active
August 18, 2019 15:34
-
-
Save afaquejam/1c717bb0f531778ccfeb75e257cff9aa to your computer and use it in GitHub Desktop.
Async Basics
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
/* | |
* An async function always returns a promise. | |
* Whatever value you return, it will be returned as a promise. | |
* If you want to reject a promise, then you can do so by throwing an error. | |
*/ | |
let getData = async (userInput) => { | |
if (userInput === 1) | |
return 'Yes!' | |
else | |
throw new Error('No! '); | |
}; | |
getData(2).then((result) => { | |
console.log(result); | |
}).catch((error) => { | |
console.log(error); | |
}); | |
/* | |
* await can only be used inside an async function. | |
* It basically takes the value received from a promise and returns it. | |
* See async-await-example.js for more details. | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment