4 ms·
Concurrency in JavaScript is simple because there are usually just 1 or 2 event sources. Event sources that are mostly idling, too. It has nothing to do with t
by mikojan 5y ago
Concurrency in JavaScript is simple because there are usually just 1 or 2 event sources. Event sources that are mostly idling, too.
It has nothing to do with threads v. no threads.
- horsawlarway 5y agoNo - concurrency is simple because the default state of running code is atomic. Variables CAN NOT change out from under you unless you explicitly place code in a callback, or await it. This is utterly untrue in threaded code, where ANY line that is not explicitly guarded or explicitly an atomic operation, might have shared data change out from underneath it. There are usually several events sources - just in a normal application - Network calls - Mouse actions - Keyboard actions - Timers - awaits/promises/callbacks - Service workers - mutation observers - postMessage events - etc... Are all viable sources of events. Some of them will trigger events incredibly often (ex - a handler registered to mouseMove or Scroll) - it has nothing to do with limited event sources, and everything to do with how the code executing the handlers is guaranteed to be atomic (unless it adds more events - such as a callback or an await)