所有的javascript回调都是异步的吗?如果不是,我怎么知道哪些是?
我很好奇是否所有 javascript 回调都是异步的,或者是否仅在某些情况下才如此.另外,我确定是什么让浏览器和 nodejs 之间的 javascript 代码异步(或使用异步 javascript 的方式)不同,所以我想知道在每种情况下什么是真正的异步 javascript.
I'm curious as to whether all javascript callbacks are asynchronous, or whether that is the case only in certain situations. Also, I'm sure what makes javascript code asynchronous (or ways to use asynchronous javascript) differ between the browser and nodejs, so I'd like to know in each situation what constitutes real asynchronous javascript.
我的印象是,在以下场景中,我实际上并不是在编写异步代码.
I'm under the impression that in the following scenarion, I'm not actually writing asynchronous code.
function addOne(value){
value = value + 1;
return value;
}
function simpleMap(values, callback){
for(i = 0; i < values.length; i++){
val = values[i];
val = callback(val);
values[i] = val;
}
return values;
}
newValues = simpleMap([1,2,3], addOne);
但是,例如,我知道 jQuery 的 AJAX 函数是真正异步的(不考虑现在可用的承诺).是什么让 jQuery 的 AJAX 异步?是不是就这么简单,涉及到XHR请求,在浏览器中,所有的XHR请求都是异步的?
However, for example, I know that jQuery's AJAX functions are truly asynchronous (not taking in to account the promises which are now available). What is it that make jQuery's AJAX asynchronous? Is it as simple that it involves XHR requests, and in the browser, all XHR requests are asynchronous?
我对 nodejs 环境有同样的问题.如果节点中的某些内容涉及文件 i/o、process.nextTick、setTimeout 或 setInterval 之类的内容,它是否只能是异步的?为什么当我使用 mongodb/mongoose 执行数据库调用之类的操作时,这是异步的吗?幕后发生了什么事情?
I have the same question for the nodejs environment. Can something in node only be asynchronous if it involves something like file i/o, process.nextTick, setTimeout, or setInterval? Why when I do something like a database call with mongodb/mongoose, is that asynchronous? What's going on behind the scenes that's making it so?
异步情况"是由环境预先确定的吗?或者有什么方法可以让自己的函数真正异步而不利用环境的非常具体的功能(例如 xhr、node 中的文件 io、process.nexttick 等)?
Are asynchronous "situations" predetermined by the environment? Or is there some way to make one's own function truly asynchronous without leveraging very specific functions of the environment (such as xhr, file io in node, process.nexttick, etc)?
推荐答案
我很好奇是否所有的 javascript 回调都是异步的
I'm curious as to whether all javascript callbacks are asynchronous
没有.例如,Array#sort
使用的回调不是异步的,String#replace
使用的回调也不是异步的.
No. For instance, the callback used by Array#sort
is not asynchronous, nor is the one used by String#replace
.
您知道回调是否是异步的唯一方法是查看它的文档.通常,涉及外部资源请求(例如 ajax 调用)的请求是异步的,而其他请求可能是异步的,也可能不是.
The only way you know whether a callback is asynchronous is from its documentation. Typically, ones involving requests for external resources (ajax calls, for instance) are asynchronous, and others may or may not be.
但是,例如,我知道 jQuery 的 AJAX 函数是真正异步的......
However, for example, I know that jQuery's AJAX functions are truly asynchronous...
不一定,因为当前 jQuery 仍然有 async
标志,您可以设置 false
来强制同步请求.(这不是一个好主意,他们将删除它,但您可以.jQuery 将标志传递给提供同步/异步行为的底层浏览器对象.)
Not necessarily, as currently jQuery still has the async
flag which you can set false
to force a synchronous request. (It's not a good idea, and they're going to remove that, but you can. jQuery passes the flag to the underlying browser object which provides the synchronous/asynchronous behavior.)
是什么让 jQuery 的 AJAX 异步?
What is it that make jQuery's AJAX asynchronous?
浏览器.jQuery 的 ajax 调用使用 XMLHttpRequest
对象(或在某些情况下,一个 script
元素),默认为浏览器提供的异步操作.
The browser. jQuery's ajax calls use the XMLHttpRequest
object (or in certain situations, a script
element), which defaults to asynchronous operation provided by the browser.
或者有什么方法可以让自己的功能真正异步,而无需利用环境中非常具体的功能...
Or is there some way to make one's own function truly asynchronous without leveraging very specific functions of the environment...
直到最近,没有.在第 5 版规范中,JavaScript 语言基本上对线程和异步的整个概念保持沉默.只有当你进入环境时它才会出现.实现异步的唯一方法是使用主机提供的函数,例如 NodeJS 上的 nextTick
(或任何异步完成的各种操作)或浏览器上的 setTimeout
.
Until recently, no. Up through the 5th edition specification, JavaScript the language was basically silent on the entire concept of threads and asynchronicity; it was only when you got into environments that it came up. The only way to make something asynchronous was to use a host-provided function, such as nextTick
(or any of the various operations that completes asynchronously) on NodeJS or setTimeout
on browsers.
在 2015 年 6 月的 ECMAScript 第 6 版规范中,他们将 promises 引入了该语言.回调通过 then
连接到 ES6 承诺,这样 总是 异步调用(即使在附加回调时承诺已经解决),因此 JavaScript 具有异步性现在在语言层面.所以如果你实现你的函数,让它返回一个promise而不是接受一个回调,你就会知道与它挂钩的then
回调将被异步触发.
In the ECMAScript 6th edition specification in June 2015, they introduced promises into the language. The callbacks hooked up to an ES6 promise via then
and such are always invoked asynchronously (even if the promise is already settled when the callback is attached), and so JavaScript has asynchronicity at a language level now. So if you implement your function so that it returns a promise rather than accepting a callback, you'll know that the then
callbacks hooked up to it will be triggered asynchronously.
相关文章