简体   繁体   English

Node.js嵌套回调

[英]Node.js nested callbacks

I have a very basic question in node.js programming . 我在node.js编程中有一个非常基本的问题。 I am finding it interesting as I have started understanding it deeper. 随着我开始更深入地了解它,我发现它很有趣。

I came across the following code : 我遇到以下代码:

Abc.do1(‘operation’,2, function(err,res1) 
{
   If(err) {
        Callback(err);
    }
    def.do2(‘operation2’,function(l) {
   }
  }

My question is : 我的问题是:

since def.do2 is written in the callback of abc.do1 , 由于def.do2是写在abc.do1的回调中的,

is it true that def.do2 will be executed after the 'operation' of abc.do1 is completed and the callback function gets called . 的确,def.do2将在abc.do1的“操作”完成并且调用回调函数之后执行。 If yes, is this a good programming practice , because we speak only asynchronous and non blocking code in node.js 如果是,这是一种好的编程习惯,因为我们在node.js中仅讲异步和非阻塞代码

Yes, you're correct. 是的,你是对的。 def.do2() is executed after abc.do1() is done. abc.do1()完成后执行def.do2()。 However, this done on purpose to make sure that do1() is done before do2() can start. 但是,这样做是为了确保do1()在do2()开始之前已完成。 If it's meant to be done in parallel, then do2() would have been outside of do1()'s callback. 如果要并行执行,则do2()会超出do1()的回调范围。 This code is not exactly blocking. 此代码未完全阻止。 after do1() is started, the code is still continuing on to execute everything below do1() functon (outside of do1's callback), just not do2() until do1() is done which is meant to be. 在do1()启动之后,代码仍然继续执行do1()函数下面的所有操作(在do1的回调之外),只是直到do1()完成才执行do2()。

Yes you are absolutely correct and you have given a correct example of callback function. 是的,您绝对正确,并且给出了正确的回调函数示例。

The main browser process is a single threaded event loop. 浏览器的主要过程是单线程事件循环。 If you execute a long-running operation within a single-threaded event loop, the process "blocks". 如果您在单线程事件循环中执行了长时间运行的操作,则该过程将“阻塞”。 This is bad because the process stops processing other events while waiting for your operation to complete. 这很不好,因为该过程在等待操作完成时停止处理其他事件。 'alert' is one of the few blocking browser methods: if you call alert('test'), you can no longer click links, perform ajax queries, or interact with the browser UI. “警告”是少数阻止浏览器的方法之一:如果调用alert('test'),则无法再单击链接,执行ajax查询或与浏览器UI交互。

In order to prevent blocking on long-running operations, the XMLHttpRequest provides an asynchronous interface. 为了防止长时间运行的操作阻塞,XMLHttpRequest提供了一个异步接口。 You pass it a callback to run after the operation is complete, and while it is processing it cedes control back to the main event loop instead of blocking. 您将操作完成后传递给它传递一个回调,以便在处理过程中将控制权移交给主事件循环而不是阻塞。

There's no reason to use a callback unless you want to bind something to an event handler, or your operation is potentially blocking and therefore requires an asynchronous programming interface. 除非您想将某些东西绑定到事件处理程序,否则您没有理由使用回调,或者您的操作可能会阻塞,因此需要异步编程接口。

See this video 观看这部影片

http://www.yuiblog.com/blog/2010/08/30/yui-theater-douglas-crockford-crockford-on-javascript-scene-6-loopage-52-min/ http://www.yuiblog.com/blog/2010/08/30/yui-theater-douglas-crockford-crockford-on-javascript-scene-6-loopage-52-min/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM