3 ms·
Javascript isn't single threaded. Callbacks happen asynchronously, and coupled with a synchronous DOM this means race condition central.
by otabdeveloper1 8y ago
Javascript isn't single threaded. Callbacks happen asynchronously, and coupled with a synchronous DOM this means race condition central.
- jcelerier 8y ago> Callbacks happen asynchronously, 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.
- otabdeveloper1 8y ago> asynchronicity and parallelism (threads) are two entirely different concepts 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.
- hawski 8y agoThe browser is the system. JavaScript is a language. With such a definition of multithreaded language wouldn't Brainfuck also be one?
- otabdeveloper1 8y agoRead what I said again. Javascript is a language that will be executed using several threads when run in the browser environment. 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'.
- jcelerier 8y ago> However, in the browser resources (such as scripts) will be loaded using multiple threads. 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-ab7d0d09ac2f https://itnext.io/how-javascript-works-in-browser-and-node-a... and do you have an example of problematic code ?