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

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

问题描述

我正在尝试掌握 Javascript 异步函数和回调.

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

我被困在回调函数的概念上,我在一些地方阅读:它们用于顺序执行代码(主要在 jquery 的上下文中,例如动画)和一些地方,特别是在 Nodejs 的上下文中;它们用于并行执行异步并避免代码阻塞.

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.

因此,该主题的某些专家能否阐明这一点并清除我脑海中的模糊(示例??).所以我可以考虑使用回调函数

或者这完全取决于您在代码中调用/放置回调函数的位置?.

谢谢,

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)

实际上这是来自互联网的例子,这让我模棱两可:

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.

然而,很容易忘记事件处理程序实际上是异步代码.附加处理程序不会调用处理程序代码,并且直到将来某个不可知的时间才会执行该代码.

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,它调用服务器.这些调用可以配置为同步,但开发人员通常更喜欢异步调用并使用回调方法来实现它们.

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 库和工具包的激增.这些努力使不同浏览器的事物实现同质化,并建立在异步代码的回调方法上.您还开始看到更多用于数组迭代或 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.

现在,我们看到了 Deferred 和 Promise 的结合.这些对象表示长时间运行的操作的值,并提供 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天全站免登陆