简体   繁体   中英

Is PURE Javascript synchronous or asynchronous?

From my understanding javascript will either run in the browser or as a backend in Node.js.

The browser or Node.js, depending on where you run your javascript, will via web API's or c++ API's handle functions that block the runtime (ie network calls, image rendering, etc), then send them to the event loop and eventually merge them into the single thread that javascript runs on.

What I don't understand is, when I google "is javascript synchronous or asynchronous", the answer is javascript is asynchronous.

But is that true? Javascript is asynchronous because of the web API's or c++ API's in the browser or Node.js backend, that makes threads under the hood, but javascript itself isn't asynchronous then?

If javascript only has one thread it must be a synchronous language?

Javascript (as implemented in the browser and in node.js) is an event driven system.

That means that it works best when used with non-blocking, asynchronous I/O that gives you the best experience and features in coordination with the event driven system. This isn't necessarily inherent in Javascript the language itself (you could make a version of Javascript that had nothing but blocking I/O), but all the popular implementations of Javascript depend upon an event queue and depend upon asynchronous I/O working in coordination with the event queue to offer a useful programming environment.

Until recently, Javascript also didn't have useful threads and useful thread synchronization tools to make a multi-threaded system with blocking I/O practical or useful. There are now threads in both the browser and node.js, though the threads in node.js are pretty heavy-handed (a whole new instance of the V8 interpreter, separate heap, etc...) so they would not necessarily be performance competitive with systems that have threads built in as more of an inherent feature. Plus the thread synchronization tools in Javascript are fairly early in their development.

What I don't understand is, when I google "is javascript synchronous or asynchronous", the answer is javascript is asynchronous.

Current popular implementations of Javascript are in environments that require asynchronous I/O in order to be productive. It's not necessarily required in the pure language all by itself, though I don't know of any implementations that assume threads and blocking I/O.

Javascript is asynchronous because of the web API's or c++ API in the browser are Node.js backend that makes threads under the hood, but javascript itself isn't asynchronous then?

A Javascript environment has asynchronous capabilities because Javascript is paired with an event driven environment and is paried with asynchronous operations such as timers and I/O. So, the combination of the Javascript implementation and the other things the environment adds to it make an environment capable of writing code that can use asynchronous features. Please don't too hung up on the semantic argument about whether Javascript is or isn't asynchronous itself. As best I know, the ECMAScript specification that specifies the Javascript language doesn't necessarily require that. I think there could exist an implementation of the pure Javascript language with no asynchronous capabilities. But, most of what you read on the web or in books will refer to "Javascript" when what they really mean are the popular implementations of Javascript such as in a web browser or in node.js. And, frankly, that's mostly what is relevant since that's where you can actually use Javascript unless you're going to build your own custom environment.

If javascript only has one thread it must be a synchronous language?

It's not entirely clear what you mean by this question. By default (without invoking webWorkers or Worker Threads) Javascript runs your Javascript code in one single thread, but it has access to non-blocking I/O functions that allow operations to run in parallel with your Javascript. In a browser, you can make an Ajax call to your server, then go do something else while that Ajax call is finishing (make some calculations, update the screen, update a clock on screen, etc...) and then when a completion notification arrives from the Ajax call, you can process the results. While your actual lines of Javascript were run one after another synchronously, you were allowed to start asynchronous operations and thus run some things in parallel with your Javascript execution. I will avoid debating whether one wants to call it a "synchronous language" or not. That's just a semantic argument. It works the way it works, running your Javascript in a single thread, but taking advantage of native OS capabilities to run other things in parallel with the Javascript (like network operations).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM