r/learnjavascript • • May 01 '20

Async vs. Sync

Post image
413 Upvotes

81 comments sorted by

View all comments

2

u/0xdead0x May 02 '20

This is true in ideal circumstances, but it’s not why you should use asynchronous functions in JavaScript. You could get results like that. You could also be on a real machine running multiple tasks that doesn’t actually have enough hardware threads to run all of your asynchronous calls in true concurrency. In JavaScript, asynchronous functions are more important as a matter of user experience. If you synchronously wait for a REST API then your app can’t respond to user input at all until it gets a response or times out. But if you do it asynchronously, then even if your machine doesn’t have a spare hardware thread, your VM’s scheduler will make sure that you’re constantly checking for a response and checking for user input such that it looks like both are happening at once.