Can we say that every function's execution in js is "atomic operation" for single thread of event loop? I mean that no 2 functions (or more) can be executed in single time
4 Answers
Javascript uses only one thread. So, at same time, it's completely impossible that 2 instructions are run in parallel. However, there is a queue of events that get fired to "jump" from one part of code to another.
BUT
A function uses more than 1 instruction, so it could happen they are running synchronized.

- 4,680
- 5
- 24
- 58
-
"they" - U meen instructions? – Vladyslav Nikolaiev Sep 02 '16 at 16:44
Yes, functions is atomic operations
To be more precise, each function is a message that is part of the queue where each message will proceed to the worker and executed to the end.
In case of non-blocking operations, most of the times small blocking operations will be executed first because required time for runtime is much larger for such things as setTimeout
with function creation.
console.log('foo');
setTimeout(function() {
console.log('bar'); // This will be called last
});
console.log('baz');

- 4,009
- 2
- 22
- 39
-
but if we replace `console.log('baz');` by the `while(true);` 'bar word' would not be shown at all – Vladyslav Nikolaiev Sep 02 '16 at 14:58
-
that's because its single threaded. If you keep that single thread busy forever, it will never go to following instruction. Thats why you never use sleep or heavy operations on not asyncro operation in javascript. You would block GUI – Mayday Sep 02 '16 at 15:03
-
@Vladuysha good point. I think it's because `while(true);` is also atomic operation and runtime wait for the end of it's execution because of [run to completion](https://developer.mozilla.org/en-US/docs/Web/JavaScript/EventLoop#Run-to-completion) principle. – G07cha Sep 02 '16 at 15:08
-
@KonstantinAzizov, it's because the scope of declaration setTimeout woud never be leaved, see more [race cond](https://blog.raananweber.com/2015/06/17/no-there-are-no-race-conditions-in-javascript/) – Vladyslav Nikolaiev Sep 02 '16 at 15:13
-
Simple answer is it's not true. Think about functions passed to setTimeout
or XmlHttpRequest.onreadystatechange
, etc. These functions will be executed at a specific or undetermined time in the future. If an function is already being executed when setTimeout kicks off or a ready state change event occurs, that function will not be waited for to finish before the setTimeout or ready state handler stats to execute.
Instead two (or more) functions can be executed by the interpreter at the same time. It may seem that they are simultaneously executed but technically that's not the case.
The first function execution is paused (after an instruction is finished and before the next would start) and instructions in the other function are executed for some time. Then this function execution is paused and the one paused before is resumed.

- 14,986
- 6
- 37
- 59
It is true, javascript is designed to be eventbased single-thread "pseudoasync" script language.
However it is something called WebWorker, it creates new process in system with given script file, so it can take 2nd thread for your application. Intrested(?) - redirecting to What are the use-cases for Web Workers?

- 1
- 1

- 1,123
- 1
- 8
- 24