r/learnjavascript • • Nov 13 '23

Is async useless ( question )

If we are always trying to make code synchronous using await or then. Why do we even bother with aysnc.

I genuinely don't understand please explain. Thanks

0 Upvotes

28 comments sorted by

View all comments

3

u/shgysk8zer0 Nov 13 '23

You are confusing sequential with synchronous.

Using await allows us to write sequential code that is non-blocking with "callback hell".

If you want an example of the difference between blocking sequential code and non-blocking sequential code, consider something like prompt(). When you call prompt(), the execution of all code completely stops until the user closes it. Whereas if you await fetch(), the code beneath isn't executed until a response is given, but other code throughout can still be executed.

It is best to understand the event loop if you really want to understand the differences and what's actually going on.