Chaining .then()
Each .then() returns a new Promise. Returning a value passes it to the next .then(); returning a Promise chains asynchronously.
Each .then() returns a new Promise. Returning a value passes it to the next .then(); returning a Promise chains asynchronously.
fetch("/api/user/1")
.then(res => res.json())
.then(user => fetch(`/api/posts?userId=${user.id}`))
.then(res => res.json())
.then(posts => render(posts))
.catch(err => showError(err));
A single .catch() at the end catches errors from any step in the chain.
More in JavaScript