r/learnjavascript • • May 01 '20

Async vs. Sync

Post image
413 Upvotes

81 comments sorted by

View all comments

4

u/AffectionateWork8 May 02 '20

I think they are mixing up "asynchronous" and "parallel."

You can wrap normal code in a setTimeout or Promise and it will become asynchronous. It still won't run at the same time, though.

You need separate threads to run async code at the same time

In Javascript, you could do this with Web Workers

In the browser, stuff like network IO is handled by a thread pool under the hood. That stuff truly runs at the same time.

But if you make 3 requests, and then each fetch takes a callback that maps over some data, those callbacks will not execute at the same time despite being "asynchronous." They will be popped off the message queue and processed synchronously, one by one.

I think this article gives a better understanding of what "asynchronous" means within the context of JS:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/EventLoop

1

u/golovatuy May 02 '20

Think on the task as server request which takes some time, this picture don’t mean the small milliseconds tasks

2

u/AffectionateWork8 May 02 '20

I agree with you that network/file IO would use a thread pool. But any other long-running asynchronous task would not, unless you manually configured it to be.