Skip to content

Instantly share code, notes, and snippets.

@emre-edu-tech
Last active October 13, 2024 07:19
Show Gist options
  • Save emre-edu-tech/a0fdccf76e2ae00873eb73f79388ed08 to your computer and use it in GitHub Desktop.
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.
// 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