Chapter XVII · Promises
Promise Chaining
Promise chaining allows sequential asynchronous operations to be linked together using `.then()` method returns.
01 How It Works
Think of an assembly line conveyor belt where each station processes data and passes it down to the next.
Every `.then()` handler returns a brand new settled promise, enabling clean sequential pipeline flows.
example.js
Promise.resolve(10)
.then(val => val * 2)
.then(result => console.log(result)); // 20
1. Call initial asynchronous promise.
2. Attach `.then()` handler transforming data.
3. Chain subsequent `.then()` or `.catch()` handlers seamlessly.
02 Practical Example
Here is how you might see this concept applied in real-world code:
practical.js
fetch("https://api.example.com/user")
.then(res => res.json())
.then(user => console.log(user.name))
.catch(err => console.error(err));
Key Takeaway
Promise chaining eliminates callback hell, providing clean readable asynchronous workflows.