Last active
October 13, 2024 07:19
-
-
Save emre-edu-tech/a0fdccf76e2ae00873eb73f79388ed08 to your computer and use it in GitHub Desktop.
This is a simple demonstration of how then()-catch() methods work while heading an api. We can see clearly then() methods are called after the previous promise resolve.
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
// Here every then() method returns a new promise | |
// then() method always yields a promise after being executed, this way chaining is possible | |
// We can say here that every then() methods waits the previous promise to resolve | |
// Error handling is also easy. If any prior promise fails, catch() method will be triggered | |
fetch('https://api.openweathermap.org/data/2.5/weather?q=London,uk') | |
.then(response => { | |
return response.json(); | |
}) | |
.then(data => { | |
console.log(data); | |
}) | |
.catch(error => { | |
console.log('There is an error!'); | |
console.log(error); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment