Last active
October 27, 2021 18:23
-
-
Save atxryan/a6ed66d92006621fc7246acc18f9d318 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// .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