简体   繁体   English

PURE Javascript 是同步的还是异步的?

[英]Is PURE Javascript synchronous or asynchronous?

From my understanding javascript will either run in the browser or as a backend in Node.js.据我了解,javascript 将在浏览器中运行或作为 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. 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 javascript 运行的单线程。

What I don't understand is, when I google "is javascript synchronous or asynchronous", the answer is javascript is asynchronous.我不明白的是,当我谷歌“javascript 是同步的还是异步的”时,答案是 javascript 是异步的。

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? 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 只有一个线程,那一定是同步语言?

Javascript (as implemented in the browser and in node.js) is an event driven system. Javascript(在浏览器和 node.js 中实现)是一个事件驱动系统。

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.这意味着它在与非阻塞、异步 I/O 一起使用时效果最佳,为您提供与事件驱动系统协调的最佳体验和功能。 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.这不一定是 Javascript 语言本身固有的(您可以制作一个只有阻塞 I/O 的 Javascript 版本),但是 Javascript 的所有流行实现都依赖于异步事件队列并依赖于异步事件队列与事件队列协调以提供有用的编程环境。

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.直到最近,Javascript 还没有有用的线程和有用的线程同步工具来使具有阻塞 I/O 的多线程系统实用或有用。 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.现在浏览器和 node.js 中都有线程,尽管 node.js 中的线程非常笨拙(V8 解释器的全新实例,单独的堆等......),因此它们不一定与系统具有竞争力将线程内置为更多的固有功能。 Plus the thread synchronization tools in Javascript are fairly early in their development.此外,Javascript 中的线程同步工具还处于开发的早期阶段。

What I don't understand is, when I google "is javascript synchronous or asynchronous", the answer is javascript is asynchronous.我不明白的是,当我谷歌“javascript 是同步的还是异步的”时,答案是 javascript 是异步的。

Current popular implementations of Javascript are in environments that require asynchronous I/O in order to be productive. Javascript 当前流行的实现是在需要异步 I/O 以提高生产力的环境中。 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.纯语言本身不一定需要它,尽管我不知道任何假设线程和阻塞 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? 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. Javascript 环境具有异步功能,因为 Javascript 与事件驱动环境配对,并与定时器和 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.因此,Javascript 实现和环境添加到其中的其他内容的组合使环境能够编写可以使用异步功能的代码。 Please don't too hung up on the semantic argument about whether Javascript is or isn't asynchronous itself.请不要过于关注 Javascript 本身是否异步的语义争论。 As best I know, the ECMAScript specification that specifies the Javascript language doesn't necessarily require that.据我所知,指定 Javascript 语言的 ECMAScript 规范不一定要求这样做。 I think there could exist an implementation of the pure Javascript language with no asynchronous capabilities.我认为可能存在没有异步功能的纯 Javascript 语言的实现。 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. 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.而且,坦率地说,这主要是相关的,因为除非您要构建自己的自定义环境,否则您可以实际使用 Javascript。

If javascript only has one thread it must be a synchronous language?如果 javascript 只有一个线程,那一定是同步语言?

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.默认情况下(不调用 webWorkers 或 Worker 线程)Javascript 在一个线程中运行您的 Javascript 代码,但它可以访问非阻塞 I/O 函数,允许操作与您的 Z9E13B69D1D2DA937Z102.AC 并行运行。 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.在浏览器中,您可以对您的服务器进行 Ajax 调用,然后 go 执行其他操作,同时 Ajax 调用完成(进行一些计算,更新屏幕,更新屏幕上的时钟)通知从 Ajax 调用到达,您可以处理结果。 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.虽然您的 Javascript 的实际行是一个接一个地同步运行,但您可以启动异步操作,从而与您的 Javascript 执行并行运行一些事情。 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).它以它的工作方式工作,在单线程中运行 Javascript,但利用本机操作系统功能与 Javascript 并行运行其他东西(如网络操作)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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