简体   繁体   English

在nodejs中,函数会在函数内的所有回调执行之前返回吗?

[英]In nodejs, will function return before all callbacks within function have executed?

  function A (req,res){
   db.query(sql,cb(err,results){
      //do something
   }
}

When I call function A in the code shown above, will it return before the cb has executed? 当我在上面显示的代码中调用function A时,它将在cb执行之前返回吗?

Not necessarily. 不必要。 If db.query is actually asynchronous, then the answer is yes. 如果db.query实际上是异步的,那么答案是肯定的。 However, many functions with callback-style signatures are not actually asynchronous, so it is possible that cb will run before A returns. 但是,许多具有回调样式签名的函数实际上并不是异步的,因此cb可能会在A返回之前运行。 You should not count on either order of events in general. 一般而言,您不应指望事件的任何顺序。

Yes. 是。

You can use async to control you sync flow. 您可以使用异步来控制同步流。

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

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