So all heavy functions should be async, like
- heavy calculations,
- input/output operations,
- system calls,
- database calls,
- updates/analyse HTML tree,
- graphics (may be)?
...
Hmm... if we create a lot of async functions, processor will try to run em all at once? It could be bad for slow computers... But some browsers are run in a single process (old firefox, for example).
Today we have users with very fast computers and usually slow smartphones...
Sorry for late response (sleep, job).
JavaScript is single threaded, there is no way in the language to manage threads. You can spinn up a lot of JS/Node processes though, but as a programmer you can manage only one.
Heavy calculations and graphics will be syncronous. You can simulate heavy calculation by running for in loop billion times. If you do it in browser, you will find, that it freezes your window for a couple of seconds. I'll try to illustrate it in later post. Good call!
Modern browsers spinn up multiple instanses for faster perfomanse. You can do it yourself with Node. But this instances have limited abylity to communicate to each other. Script you write will only be executed in one thread.
Threads is an instrument to solve concurrency problem in programming, JavaScript solved concurrency by build in async. There mechanism under the hood called Event Loop, it some sort of the queue, which Javascript uses to know when to run your callback.
Thank you! It was very informative,
I am programming usually on perl and my knowledge about javascript is tiny, only base syntax and jquery.