4 ms·
> Async/await with one thread is simple and well-understood. That's the Javascript model. Bit of nuance (I'm not an authority of this and I don't know the curr
by MuffinFlavored 3y ago
> Async/await with one thread is simple and well-understood. That's the Javascript model.
Bit of nuance (I'm not an authority of this and I don't know the current up-to-date answer across 'all' of the Javascript runtimes these days):
Isn't it technically "the developer is exposed the concept of just one thread without having to worry about order of execution (other than callbacks/that sort of pre-emption)" but under the hood it can actually be using as many threads as it wants (and often does)? It's just "abstracted" away from the user.
- recursive 3y agoI don't think so. Two threads updating the same variable would be observable in code. There's no way around it.
- MuffinFlavored 3y agoI'm referring to something like this: https://stackoverflow.com/questions/7018093/is-nodejs-really-single-threaded https://stackoverflow.com/questions/7018093/is-nodejs-really... It's like a pedantic technical behind the scenes point I think, just trying to learn "what's true"
- recursive 3y agoLooks like the spec refers to the thing-that-has-the-thread as an "agent". https://tc39.es/ecma262/#sec-agents https://tc39.es/ecma262/#sec-agents I don't know the details about how any implementation of a javascript execution environment allows for the creation of new agents.
- kaba0 3y agoI mean, yeah, if you go deep enough your OS may decide to schedule your browser thread to a different core as well. I don’t think it has any relevance here - semantically, it is executed on a single thread, which is very different from multi-threading.
- MuffinFlavored 3y agoIt is executed by your runtime which may or may not behind the scenes be using a single thread for your execution and/or the underlying I/O/eventing going on underneath, no?
- throwitaway1123 3y agoMost JS runtimes are multi-threaded behind the scenes. If you start a node process: node -e "setTimeout(()=>{}, 10_000)" & Then wait a second and run: ps -o thcount $! Or on macOS: ps -M $! You'll see that there are multiple threads running, but like others have said, it's completely opaque to you as a programmer. It's basically just an implementation detail.
- throwitaway1123 3y agoIf you need true parallelism of course you can opt-in to Web Workers (called Worker Threads in Node), or the Node-specific child_process.fork, or cluster module.