Skip to content

Instantly share code, notes, and snippets.

@rajatjain-21
Created September 8, 2022 03:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rajatjain-21/f7159bd7483b85f7b9e4d2a3f6674649 to your computer and use it in GitHub Desktop.
Save rajatjain-21/f7159bd7483b85f7b9e4d2a3f6674649 to your computer and use it in GitHub Desktop.
Async Example
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