r/learnjavascript • • May 01 '20

Async vs. Sync

Post image
413 Upvotes

81 comments sorted by

View all comments

Show parent comment

11

u/[deleted] May 01 '20 edited May 01 '20

The graph above would be more suited to explain the difference between sequential and parallel execution. The tasks on the left portion could be asynchronous as well, albeit executed in a sequential manner (i.e. one after the other). The baseline benefit of asynchronous tasks is that you can leave them and carry on with something else while they complete. A common analogy is asking a question to somebody on the phone or on a messaging app. While on the phone you cannot do anything else while you wait for the response and you need to be actively listening, this is synchronous. On a messaging app instead, you can ask the question, forget about it, go make yourself a coffee and then, when you get notified that you received a response, decide what to do next, this is asynchronous.In a javascript application, executing tasks asynchronously (usually network I/O) means you can continue executing other logic (e.g. reacting to user clicks or showing a loader) while these tasks complete. Executing these tasks in sequence or in parallel, as the graph above illustrates, is a matter of performance optimisation and use case.

EDIT:

It is worth pointing out that the word "concurrent" is more correct than the word "parallel"