ELI5: The difference between asynchronous and synchronous function executions and tasks, specifically within Javascript?
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
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.
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.
Thank you for expanding on it. Building upon the example helped lots.
Thank you!