ELI5: The difference between asynchronous and synchronous function executions and tasks, specifically within Javascript?

Explained

Hiya!

I'm looking to get a strong grasp on the difference between asynchronous and synchronous function executions, specifically within JavaScript and the modularization of code?

Posted Dec 10, 2012, 9:54 PM UTC

06
[deleted]•

Synchronous is like going to the pizza place, making an order, waiting for the order, then taking your pizza and going home.

Asynchronous is like calling the pizza place, saying "Hey, can I get a large pepperoni", then in 20 min a pizza shows up at your door.

3
EdgeOfDreams•

To take this analogy further, let's say you want pizza, but your wife wants Chinese food and your kids want burgers.

Synchronous calls would be like going to the pizza place, order, wait, take the pizza, go to the Chinese place, order, wait, take the food, go to the burger place, order, wait, take the food home. This takes a long time, but you at least know for sure that each order was completed properly before you go on to the next one.

Asynchronous would be like calling the pizza place and ordering delivery. Then call the Chinese place and order delivery. Then call the burger place and order deliver (yes, delivery burgers exist). Then wait. All 3 will show up (eventually, you hope), but you don't know what order they will show up in.

4
RobbieHartOP•

Thank you for expanding on it. Building upon the example helped lots.

1
RobbieHartOP•

Thank you!

1
Amarkov•

Synchronous functions will always happen in the order that you told them to. That is, one function will not start until the last one has finished.

Asynchronous functions won't wait for other functions to start or finish; they'll just do what the function does as fast as they feel like doing it.

3
RobbieHartOP•

Thank you!

1