Hello Im Jofu from and I’m struggling to manage asynchronous functions in JavaScript, especially with nested callbacks. I’ve heard about promises and async/await, but I’m not sure which is better for my case. Can someone explain the differences and help me decide when to use each?
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.
Sign up for Infrastructure as a Newsletter.
Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Hey Jofu 👋,
Managing asynchronous code can definitely be challenging, especially when dealing with nested callbacks, which often leads to what’s called “callback hell.”
I would recommend starting with this tutorial here:
This will give you a good understand! Besides that, here are a few things that you should know:
1. Callbacks
Callbacks are the original way to handle asynchronous tasks, but they can get messy with deeply nested functions:
As you can see, this nesting can quickly become hard to manage.
2. Promises
Promises help make async code easier to read by flattening it. They represent a value that will be available at some point (resolved or rejected):
Promises help with readability and avoid deeply nested callbacks, but handling asynchronous code in a linear, synchronous-looking way is where async/await shines.
3. Async/Await
Async/await is built on top of promises, but it allows you to write asynchronous code in a more “synchronous” style. It makes your code easier to read and maintain:
Key Differences:
When to use Promises:
When to use Async/Await:
Both are great tools, but for most cases nowadays, async/await tends to be easier to work with once you’re familiar with it. If you’re new to this, start with promises and gradually work towards using async/await.
Let me know if you need more help!
- Bobby