In the previous post, we saw how Promises fix Callback Hell.
But sometimes even this:
makeTea()
.then(addSugar)
.then(addMilk)
.then(boilTea)
.then(serveTea)
.catch(handleError);
still feels a little technical.
This is where async/await makes things even simpler.
Real-life example (Chai Shop ☕)
Imagine the chai wala says:
“I will prepare your tea step by step.
Just wait.”
Now he simply:
- Makes tea
- Adds sugar
- Adds milk
- Boils it
- Serves it
Everything happens in order.
No confusion. No chaining.
This is how async/await works.
Promise version
makeTea()
.then(addSugar)
.then(addMilk)
.then(boilTea)
.then(serveTea)
.catch(handleError);
async/await version
async function prepareTea() {
try {
await makeTea();
await addSugar();
await addMilk();
await boilTea();
await serveTea();
} catch (error) {
handleError(error);
}
}
Why async/await feels easier
- Code looks like normal step-by-step instructions
- No chaining
- Easier to debug
- Cleaner error handling with try/catch
One-line summary
- async/await makes Promise-based code look like simple, straight steps.
## Next Question for you Guys 👇
👉 What is Debouncing in JavaScript, and why do we need it?
Top comments (0)