如何异步JavaScript的执行情况?当不使用return语句? [英] How does Asynchronous Javascript Execution happen? and when not to use return statement?

查看:177
本文介绍了如何异步JavaScript的执行情况?当不使用return语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

// synchronous Javascript
var result = db.get('select * from table1');
console.log('I am syncronous');

// asynchronous Javascript 
db.get('select * from table1', function(result){
    // do something with the result
});
console.log('I am asynchronous')

我知道在同步code,执行console.log()执行结果后从数据库中取出,而在异步code的console.log()执行db.get方法就会()获取结果之前。

I know in synchronous code, console.log() executes after result is fetched from db, whereas in asynchronous code console.log() executes before the db.get() fetches the result.

现在我的问题是,如何执行发生在后台为异步code后面,为什么它非阻塞?

Now my question is, how does the execution happen behind the scenes for asynchronous code and why is it non-blocking?

我已搜查ECMAScript的5个标准,以了解如何异步code ++工程,但找不到整个标准字异步的。

I have searched the Ecmascript 5 standard to understand how asynchronous code works but could not find the word asynchronous in the entire standard.

和来自nodebeginner.org我还发现,我们不应该使用return语句,因为它阻止事件循环。但API的NodeJS和第三方模块包含到处return语句。所以,当应该return语句中使用时不应该吗?

And from nodebeginner.org I also found out that we should not use a return statement as it blocks the event loop. But nodejs api and third party modules contain return statements everywhere. So when should a return statement be used and when shouldn't it?

有人可以抛出一些光在这?

Can somebody throw some light on this?

推荐答案

首先,通过一个函数作为参数,告诉你打电话,你想它来调用这个函数一段时间在未来的作用。当正好在今后将被调用取决于什么样的功能是做的本质。

First of all, passing a function as a parameter is telling the function that you're calling that you would like it to call this function some time in the future. When exactly in the future it will get called depends upon the nature of what the function is doing.

如果该功能是做一些网络,功能配置为非阻塞或异步,那么函数将执行,网络操作将被启动,并且调用的函数将返回马上和你在线的其余部分JavaScript的code后该功能将被执行。如果从函数返回一个值,它会立即返回,你长作为参数传递被称为函数之前(网络操作尚未完成)。

If the function is doing some networking and the function is configured to be non-blocking or asychronous, then the function will execute, the networking operation will be started and the function you called will return right away and the rest of your inline javascript code after that function will execute. If you return a value from that function, it will return right away, long before the function you passed as a parameter has been called (the networking operation has not yet completed).

同时,网络操作将在这个背景。它的发送请求,侦听该响应,则收集的响应。当网络请求完成和响应已收集,那时,也只有那么做,你叫原来的函数调用你作为参数传递的功能。这可能是只有几毫秒以后或者它可以是只要分钟后 - 这取决于走上完成网络操作多久

Meanwhile, the networking operation is going in the background. It's sending the request, listening for the response, then gathering the response. When the networking request has completed and the response has been collected, THEN and only then does the original function you called call the function you passed as a parameter. This may be only a few milliseconds later or it may be as long as minutes later - depending upon how long the networking operation took to complete.

明白的,重要的是,在你的榜样,在 db.get方法就会()函数调用早已完成后,它也执行code会。什么未完成的是你​​作为一个参数的函数传递内部匿名函数。这是被关押在一个javascript函数闭包,直到当联网功能完成后。

What's important to understand is that in your example, the db.get() function call has long since completed and the code sequentially after it has also executed. What has not completed is the internal anonymous function that you passed as a parameter to that function. That's being held in a javascript function closure until later when the networking function finishes.

这是我认为有一件事混淆,很多人是匿名函数您的呼叫db.get方法就会的内部声明,似乎成为其中的一部分,似乎当 db.get方法就会()完成后,这将被过于进行,但是事实并非如此。也许这看起来不怎么样,如果是重新presented是这样的:

It's my opinion that one thing that confuses a lot of people is that the anonymous function is declared inside of your call to db.get and appears to be part of that and appears that when db.get() is done, this would be done too, but that is not the case. Perhaps that would look less like that if it was represented this way:

function getCompletionfunction(result) {
    // do something with the result of db.get
}

// asynchronous Javascript 
db.get('select * from table1', getCompletionFunction);

然后,也许它会更明显,db.get方法就会将立即返回并getCompletionFunction将调用在未来的一段时间。我不建议你写这种方式,但只是作为展示说明究竟发生什么事的一种手段这种形式。

Then, maybe it would be more obvious that the db.get will return immediately and the getCompletionFunction will get called some time in the future. I'm not suggesting you write it this way, but just showing this form as a means of illustrating what is really happening.

下面是一个序列值得理解:

Here's a sequence worth understanding:

console.log("a");
db.get('select * from table1', function(result){
    console.log("b");
});
console.log("c");

你会怎样在调试器控制台中看到的是这样的:

What you would see in the debugger console is this:

a
c
b

一首先发生。然后,db.get方法就会()开始它的操作,然后立即返回。因此,C接下来发生。然后,当db.get方法就会()操作在未来实际完成一定的时间,b的情况发生。

"a" happens first. Then, db.get() starts its operation and then immediately returns. Thus, "c" happens next. Then, when the db.get() operation actually completes some time in the future, "b" happens.

有关如何处理异步浏览器中工作的一些阅读,请参阅How没有JavaScript的处理在后台AJAX响应?

For some reading on how async handling works in a browser, see How does JavaScript handle AJAX responses in the background?

这篇关于如何异步JavaScript的执行情况?当不使用return语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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