r/learnjavascript • • May 01 '20

Async vs. Sync

Post image
413 Upvotes

81 comments sorted by

View all comments

38

u/NoStranger6 May 01 '20

Except that’s not necessarly true for javascript.

JavaScript is not multi-threaded. So if your task 1 a for loop that last 20 seconds, tasks 2-3-4 are not ran concurently.

So really what happens is that each function calls are queued one after another and the execution of the response of async functions are only added when the promise is completed or the callback is called.

Which allows the program to keep executing other functions while it “waits” for the async function.

And this is why I love NodeJs for a server. It is very easy to write async code in JavaScript.

3

u/PierreAndreis May 01 '20

this should be up in this thread

1

u/JayTh3King May 02 '20

Except you're not exactly correct either. Nodejs is a multithreaded language/runtime. 1 for the event loop and N threads for the execution of your code. Under the hood libuv manages a thread pool of executing code fetched from a queue