JavaScript是同步(阻塞)或异步(非阻塞)默认 [英] Is Javascript synchronous(blocking) or Asynchronous(nonblocking) by default

查看:228
本文介绍了JavaScript是同步(阻塞)或异步(非阻塞)默认的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图把握JavaScript的异步函数和回调。

I am trying to grasp on Javascript Asynchronous functions and callbacks.

我就死在回调函数,在那里我读了一些地方的观念:他们是用有code(主要是jQuery中如动画的上下文中)的顺序执行,一些地方特别是在上下文的NodeJS;他们是用有一个并行执行异步并避免code阻塞。

I got stuck on the concept of callback functions, where I am reading on some places: they are use to have sequential execution of code (mostly in context of jquery e.g animate)and some places specially in the context of Nodejs; they are use to have a parallel execution Asynchronous and avoid blocking of code.

所以可以在这个主题中的一些专家,请在此阐明,并在我的脑海里(例子?)清除此细毛。
 所以我可以让我的脑海回调函数的使用

或者说是完全取决于你在哪里调用/放置一个回调函数在code的地方吗?

谢谢,

P.S:我很害怕,这个问题会接近主观但我仍然可以期待这(也许一些例子)具体的答案

P.S: I am scared that this question would be close as subjective but still I could expect concrete answer for this (perhaps some examples)

编辑:其实这是从互联网上这点让我ambigous的例子:

actually this is the example from internet which makes me ambigous:

function do_a(){
  // simulate a time consuming function
  setTimeout( function(){
    console.log( '`do_a`: this takes longer than `do_b`' );
  }, 1000 );
}

function do_b(){
  console.log( '`do_b`: this is supposed to come out after `do_a` but it comes out before `do_a`' );
}

do_a();
do_b();

结果

`do_b`: this is supposed to come out after `do_a` but it comes out before `do_a`
`do_a`: this takes longer than `do_b`

当JS是连续的,则do_b应该总是根据我的了解来do_a后。

when JS is sequential then do_b should always come after do_a according to my understanding.

推荐答案

的JavaScript的核心主要是同步的,在功能全面完成自己的任务,在完成之前。以AJAX的出现之前,它真的只有setTimeout和setInterval的所提供异步行为。

The core of JavaScript is largely synchronous, in that functions complete their task fully, before completing. Prior to the advent of AJAX, it was really only setTimeout and setInterval that provided asynchronous behavior.

然而,这是很容易忘记,事件处理程序,有效地异步code。附加处理程序不调用句柄code,直到在未来一段时间不可知不执行的code。

However, it's easy to forget that event handlers are, effectively async code. Attaching a handler does not invoke the handler code and that code isn't executed until some unknowable time in the future.

然后来到AJAX,其调用给服务器。这些调用可以被配置为是同步的,但开发商普遍preferred异步调用和使用回调方法实现它们。

Then came AJAX, with its calls to the server. These calls could be configured to be synchronous, but developers generally preferred async calls and used callback methods to implement them.

然后,我们看到JS库和工具包的扩散。这些力图同质化不同的浏览器的东西实现,并建立在回调方法异步code。你也开始看到的东西像数组迭代或CSS查询结果的处理更大量的同步回调。

Then, we saw the proliferation of JS libraries and toolkits. These strove to homogenize different browsers' implementations of things and built on the callback approach to async code. You also started to see a lot more synchronous callbacks for things like array iteration or CSS query result handling.

现在,我们看到在混合Deferreds和承诺。这些是重新present长时间运行的操作的值,并用于当它到达处理该值提供的API的对象。

Now, we are seeing Deferreds and Promises in the mix. These are objects that represent the value of a long running operation and provide an API for handling that value when it arrives.

倾斜的NodeJS朝着一个异步的方式很多东西;这是非常真实。然而,这更多的是对他们的部分设计决定的,而不是JS的任何固有的异步性。

NodeJS leans towards an async approach to many things; that much is true. However this is more a design decision on their part, rather than any inherent async nature of JS.

这篇关于JavaScript是同步(阻塞)或异步(非阻塞)默认的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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