mthanh2209 / javascript-training

https://javascript-course-three.vercel.app
0 stars 0 forks source link

Question 6 #6

Open mthanh2209 opened 1 year ago

mthanh2209 commented 1 year ago

Compare Async and Sync and when to use?

mthanh2209 commented 1 year ago
  1. The differences between asynchronous and synchronous include:
    • Async is multi-thread, which means operations or programs can run in parallel.
    • Sync is single-thread, so only one operation or program will run at a time.
    • Async is non-blocking, which means it will send multiple requests to a server.
    • Sync is blocking — it will only send the server one request at a time and will wait for that request to be answered by the server.
    • Async increases throughput because multiple operations can run at the same time.
    • Sync is slower and more methodical.
  2. When to use async programming
    • Asynchronous programming should only be used in programming independent tasks, where it plays a critical role.
    • Ex: a shopping app. When a user pulls up their order, the font size should increase. Instead of first waiting to load the history and update the font size, asynchronous programming can make both actions happen simultaneously.
  3. When to use sync programming
    • code is easier to write and doesn’t require tracking and measuring process flows (as async does). Because tasks depend on each other, there’s a need to know if they could run independently without interrupting each other.
    • Ex: When checking out online, a user wants to buy all of their items together, not individually. Instead of completing an order every time the user adds something to their cart, synchronous programming ensures that the payment method and shipping destination for all items are selected at the same time.