Skip to content

Instantly share code, notes, and snippets.

@atxryan
Last active October 27, 2021 18:23
Show Gist options
  • Save atxryan/a6ed66d92006621fc7246acc18f9d318 to your computer and use it in GitHub Desktop.
Save atxryan/a6ed66d92006621fc7246acc18f9d318 to your computer and use it in GitHub Desktop.
// .then() chaining
fetch("https://jsonplaceholder.typicode.com/users/1") //1
.then((response) => response.json()) //2
.then((user) => {
console.log(user.address); //3
});
// .then() callback
const address = fetch("https://jsonplaceholder.typicode.com/users/1")
.then((response) => response.json())
.then((user) => {
return user.address;
});
const printAddress = () => {
address.then((a) => {
console.log(a);
});
};
printAddress();
// using async / await
const address = fetch("https://jsonplaceholder.typicode.com/users/1")
.then((response) => response.json())
.then((user) => {
return user.address;
});
const printAddress = async () => {
const a = await address;
console.log(a);
};
printAddress();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment