There are multiple approaches for handling asynchronous operations in Javascript. Some of the most common ways include Callback functions, Promises, and Async/Await.
Which one do you prefer to use and why?
We're a place where coders share, stay up-to-date and grow their careers.
There are multiple approaches for handling asynchronous operations in Javascript. Some of the most common ways include Callback functions, Promises, and Async/Await.
Which one do you prefer to use and why?
Oz Ramos -
Steve Sewell -
Rishabh Singh -
Nader Dabit -
Discussion
For me all of them have a different purpose. By default, if you want sequential code: Async/Await. More readable.
If you are in an event driven code (with a trigger that can be called several time), you have to use callbacks.
In some very specific cases, I use promises, for instance to have a queue of operation
I agree. Everything has it’s best use case and nothing is the best tool for everything.
I use promised more out of not being fully comfortable with async/await yet, even though async/await seems like the better tool for the job in a lot of cases. But I always hated callbacks, so anything else is better. 😄
async
-await
- but sometimes it just makes sense to use athen
with a traditionalPromise
.We should remember that
async
is nothing but a 'wrapper' or syntacic sugar for aPromise
- it's not wholly different thing.async
just makes our code a bit easier to read as it 'looks' 👀 synchronous. And, no callback is needed, mostly. 👍🏾Callbacks
are generally just for 'event based' UI stuff, such asaddEventListener
.Callbacks always leads to callback hell. If I start something and end up needing more than 3-4 asynchronous calls, I will always refactor to async/await if for some reason I start up with callbacks (it happens, particularly if I start from an example).
I actually jumped from callbacks to async/await. Promises for some unknown reason just didnt capture the "full fix" to the problem for me. I had battled callbacks for a long time and I think smaller, cleaner functions ended up reducing the amount of callback hell I hit in a single implementation. I started to notice posts on various blogs discussing the endless .then world too.
In the end when async/await came along it felt like the most natural method to writing code, without the nesting and ultimately more readable.
Async await when there are multiple async actions, but I don't mind using callbacks for simple stuff like running a function after 2 seconds seconds using useTimeout.
Well, Promises and Async/Await are basically the same, so I use both of them but I don't use callbacks because I don't like them.
RxJS, Async/Await, Promises, callback functions. This is my list of tools composed by preference priority.
I prefer to use Async/Await.
Why?
It feels more natural to handle asynchronous calls in this way. It also saves me from having a nested structure which sometimes happens when using callbacks. And I think that Async/Await uses a cleaner syntax than the other implementations. It makes async calls look more like synchronous javascript, so it's easier to understand.
So, that's why I am a big fan of the Async/Await way of handling async calls! ❤
+1 — The code is cleaner and I won’t end in callback hell. Or have strangely nested Promises because I need values from various responses.