是否所有的JavaScript回调异步?如果没有,我怎么知道这是? [英] Are all javascript callbacks asynchronous? If not, how do I know which are?

查看:107
本文介绍了是否所有的JavaScript回调异步?如果没有,我怎么知道这是?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很好奇的是,所有的JavaScript回调是否是异步的,还是这只是在某些情况下的情况。另外,我敢肯定,是什么使JavaScript的code异步(或方式使用异步JavaScript)的浏览器和之间的NodeJS不同,所以我想在什么是真正的异步JavaScript每一种情况就知道了。

我是IM pression在以下scenarion,我没有实际编写异步code下。

 函数addOne(值){
  值=价值+ 1;
  返回值;
}功能simpleMap(数值,回调){
  对于(i = 0; I< values​​.length;我++){
    VAL =值[I]
    VAL =回调(VAL);
    值[I] = VAL;
  }
  返回值;
}newValues​​ = simpleMap([1,2,3],addOne);

然而,例如,我知道,jQuery的AJAX功能是真正的异步(不考虑中占其中现已承诺)。是什么,让jQuery的AJAX异步?它是那样简单,它涉及XHR请求,并在浏览器中,所有XHR请求都是异步的?

我对环境的NodeJS同样的问题。东西在节点只能是异步的,如果它涉及到的东西,如文件I / O,process.nextTick,的setTimeout或setInterval的?为什么当我做类似的MongoDB用/猫鼬数据库调用,是异步的?这是怎么回事说的做会这样呢?

幕后

是异步的情况,由环境pdetermined $ P $?或者是有一些方法,使自己真正的功能不同步利用环境非常具体的功能(如XHR,在节点的文件IO,process.nexttick等)?


解决方案

  

我很好奇,所有的JavaScript回调,是否是异步的。


没有。例如,使用阵列#排序回调不是异步的,也不是由中所使用的字符串#替换

您知道是否回调是异步的唯一方法是从它的文档。通常情况下,涉及到对外部资源的请求(AJAX调用,例如)的是异步的,并且其他人可能是或不是


  

然而,例如,我知道,jQuery的AJAX功能是真正的异步...


不一定,因为目前的jQuery仍具有异步标志,你可以设置来强制同步请求。 (这不是一个好主意,他们会删除,但你的可以的。jQuery的传递标志底层浏览器对象,它提供的同步/异步行为。)


  

这是什么使jQuery的AJAX异步?


浏览器。 jQuery的Ajax调用使用 XMLHtt prequest 对象(或在某些情况下,一个剧本元素),默认由浏览器所提供的异步操作。


  

或者是有一些方法,使自己真正的功能不同步利用环境非常具体的功能...


直到最近,没有。向上穿过第5版规范,JavaScript的的语言的是在线程和异步性的整体概念基本上是沉默的;这是只有当你坐进它想出了环境。使一些异步的唯一方法是使用主机提供的功能,如 nextTick (或任何异步完成各种操作的)上的NodeJS或的setTimeout 上的浏览器。

在2015年6月ECMAScript的第6版规范,他们推出的承诺的成语言。通过迷上了一个ES6承诺的回调然后键,如总是的异步(即使回调时附加的承诺已经解决)调用等的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);

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?

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?

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)?

解决方案

I'm curious as to whether all javascript callbacks are asynchronous

No. For instance, the callback used by Array#sort is not asynchronous, nor is the one used by String#replace.

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.

However, for example, I know that jQuery's AJAX functions are truly asynchronous...

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.)

What is it that make jQuery's AJAX asynchronous?

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...

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.

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.

这篇关于是否所有的JavaScript回调异步?如果没有,我怎么知道这是?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆