Skip to content

Instantly share code, notes, and snippets.

@afaquejam
Last active August 18, 2019 15:34
Show Gist options
  • Save afaquejam/1c717bb0f531778ccfeb75e257cff9aa to your computer and use it in GitHub Desktop.
Save afaquejam/1c717bb0f531778ccfeb75e257cff9aa to your computer and use it in GitHub Desktop.
Async Basics
/*
* 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