Promise chaining is a technique in JavaScript where multiple asynchronous operations are executed one after another, with each operation returning a Promise
that passes its result to the next .then()
in the chain. This allows you to perform a series of operations in a structured and readable way, avoiding callback nesting (also known as "callback hell").
Here’s a simple example that demonstrates promise chaining:
fetchData()
returns a promise that resolves after 1 second.fetchData()
is passed to processData()
using .then()
chaining.processData()
returns a promise that resolves after 1 second with the processed data.processData()
is passed to displayData()
using another .then()
call..catch()
block will handle the error.
.catch()
at the end.