5 ms·
No, Javascript has no notion of threads. Rather than true concurrency and a synchronization mechanism, it has callbacks (upon callbacks upon callbacks).
by svachalek 13y ago
No, Javascript has no notion of threads. Rather than true concurrency and a synchronization mechanism, it has callbacks (upon callbacks upon callbacks).
- YcombRegBroken 13y agoCallbacks all the way down. I blame 42% of my recent gray hair on callbacks.
- jbmartin 13y agoTechnically speaking, HTML 5 introduced web-workers which allows javascript to have multiple threads (see 1). However, it's still useful to think about them as being single threaded since workers are sandboxed, meaning they don't have access to the global namespace. This avoids issues commonly found in concurrent programming. E.g., separate threads cannot read and write to the same variable since they don't have access to each other's namespaces. [1] https://developer.mozilla.org/en-US/docs/Web/Guide/Performance/Using_web_workers https://developer.mozilla.org/en-US/docs/Web/Guide/Performan...
- thedufer 13y agoWhile that's true, I don't believe it's applicable to node.js, so it's a bit out of scope for this article.
- jbmartin 13y agoPeople coming from multi-threaded server environments should be aware that multi-threading does in fact exist in Node (scope of parent comment), but it's rather different from other more common concurrent event handling schemes. I.e., you cannot have shared mutable variables across threads (scope of article and ongoing async discussion). For examples, see https://npmjs.org/package/webworker-threads https://npmjs.org/package/webworker-threads and https://github.com/cramforce/node-worker https://github.com/cramforce/node-worker.
- thedufer 13y agoFascinating. I didn't know that webworkers were available in node.js. I stand corrected, then.
- anaphor 13y agoYou can create lightweight threads built on delimited continuations in javascript, same as any language that supports thunks reified as functions.