DEV Community

loading...
Cover image for Do you prefer callbacks, promises, or async/await?

Do you prefer callbacks, promises, or async/await?

madza profile image Madza ・1 min read

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?

Discussion

pic
Editor guide
Collapse
qmenoret profile image
Quentin Ménoret

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

Collapse
raguay profile image
Richard Guay

I agree. Everything has it’s best use case and nothing is the best tool for everything.

Collapse
ben profile image
Ben Halpern

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. 😄

Collapse
codefinity profile image
Manav Misra

async-await - but sometimes it just makes sense to use a then with a traditional Promise.
We should remember that async is nothing but a 'wrapper' or syntacic sugar for a Promise - 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 as addEventListener.

Collapse
andreidascalu profile image
Andrei Dascalu

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).

Collapse
imthedeveloper profile image
ImTheDeveloper

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.

Collapse
promikecoder2020 profile image
ProMikeCoder2020

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.

Collapse
shadowtime2000 profile image
shadowtime2000

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.

Collapse
dpishchalka profile image
Dmitry Pishchalka

RxJS, Async/Await, Promises, callback functions. This is my list of tools composed by preference priority.

Collapse
codewithfahad profile image
Fahad Imran

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! ❤

Collapse
roka profile image
Robert Katzki

+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.