Good question!
If you using libraries or native functions with async API than you have no choice, but use it in asynchronous manner.
Native setTimeout and setInterval could be used only by providing callback.
Making network request with Ajax library always asynchronous. You never know than you will get response. It is possible to make synchronous request, but it will freeze browser for user, so it is bad practice.
Read data from disk or database is slow, better to do it async style with Node JS.
All user interactions are asynchronous in nature. You never know then he presses a button.
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.
Good question!
If you using libraries or native functions with async API than you have no choice, but use it in asynchronous manner.
Native setTimeout and setInterval could be used only by providing callback.
Making network request with Ajax library always asynchronous. You never know than you will get response. It is possible to make synchronous request, but it will freeze browser for user, so it is bad practice.
Read data from disk or database is slow, better to do it async style with Node JS.
All user interactions are asynchronous in nature. You never know then he presses a button.
So all heavy functions should be async, like
...
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.