by otabdeveloper1 · 8y ago · 0 comments
asynchronicity and parallelism (threads) are two entirely different concepts. Most GUI systems except BeOS's are asynchronous on a single thread. When you code in node you just have one big hidden while loop that does
while(true) {
doPostedEvents();
}
and executes your callbacks one after each other.They are. However, in the browser resources (such as scripts) will be loaded using multiple threads. Javascript will be executed on one thread at a time, but this execution will switch to another context at the appropriate breakpoints.
Javascript in the browser is a multithreaded cooperatively multitasking system.
Are you sure about this ? I could not find anywhere something saying that the main JS execution would switch threads - and even if it did, it would not impact the observable behaviour of your JS code anyways. Everything I could find on google says "JS is single-threaded" (except for web workers of course).
Is this page wrong ? : https://itnext.io/how-javascript-works-in-browser-and-node-a... and do you have an example of problematic code ?
With such a definition of multithreaded language wouldn't Brainfuck also be one?
However, that isn't the point. The point is that multiple Javascript scripts will be executing concurrently (via cooperative multitasking) and manipulating the same DOM elements.
This is absolutely a source of race conditions and a huge, HUGE pain in the ass for programmers who think Javascript is a 'single-threaded language'.