Promise Chaining
Return a value or another promise from .then to build a clear chain.
Nested .then calls are hard to read. Prefer a
chain: each .then returns either a normal
value (passed to the next step) or another promise (the chain waits for it).
One .catch at the end handles any rejection in the chain.
Steps appear here.
Important JavaScript
// Flat chain — preferred
fetchId(1)
.then(function (id) {
return fetchPlayer(id); // return a promise → wait for it
})
.then(function (player) {
return player.name + " (" + player.rating + ")"; // return a value
})
.then(function (label) {
console.log(label);
})
.catch(function (error) {
console.log(error.message);
});