在 Nodejs 中,如果你先调用 cb,那么这会导致函数的其余部分执行吗?

In Nodejs, if you call the cb first, then will this cause the rest of the function to execute?

本文关键字:函数 执行 余部 如果 Nodejs 调用 cb      更新时间:2023-09-26

假设我有

function foo(cb) {
  cb()
}
function bar() {
  foo(function() {});
}
function zee(cb) {
  bar()
  cb()
}

如果我调用 zee,对 zee cb 的调用会等待酒吧的 foo cb 执行吗?

是的

- 除非你使用任何异步代码(如setTimeout、AJAX请求或承诺),否则你的程序将逐个执行调用。

zee(cb1)
    bar()
        foo(cb2)
            cb2()
    cb1()

请注意,这不是特定于 Node,这只是 JavaScript 的一般工作方式。