Promise Chaining in JavaScript!
Ujjwal Kumar
B.Tech???? | DCE'25 CSE | MERN Stack | DevOps | DSA(Java) | LeetCode | Technical Writer
Promise chaining is a technique for handling asynchronous operations in a sequential manner using .than() and .catch() method. It allows to create a chain of promises, where the result of one promise is passed to the next as input. This helps to avoid callback hell a situation where nested callback become difficult to read and manage.
In the code, the fetchData function returns a promise that resolves with data after a simulated delay.The first .than() attaches a callback to the fetchData promise. This callback receives the resolved data(data).
.
In the code, inside the callback, we call processData(data), which also returns a promise. The second .than() attaches another callback to the processData promise. This callback receives the processed data
Inside the second callback, we log the uppercase processed data. the .catch() method is used to handle any errors than might occur in the promise chain.