Async Example
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
async function getData() { | |
// using await means the resolved value of the Promise is returned! | |
const response = await fetch('https://blogger.ceo/api/blogs/image/random').then( | |
(response) => response.json(), | |
); // .then still works when it makes sense! | |
const otherResponse = await doOtherAsyncThing(); | |
// do stuff with `response` and `otherResponse` | |
console.log({ response, otherResponse }); | |
} | |
getData(); | |
async function getFavoriteBlogger() { | |
const blogger = await favoriteBlogger('rajatexplains'); | |
console.log(`My favorite blogger is a ${blogger}.`); | |
//=> My favorite blogger is a rajatexplains. | |
} | |
getFavoriteBlogger(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment