简体   繁体   English

NodeJS 中的回调

[英]Callbacks in NodeJS

I have an async function我有一个异步功能

function(word,callback)
{
  setTimeout(3000, function()
  {
      console.log("waiting");
  }
  callback(null, word);
}

function f2 (err, result){
  console.log(result);
}

What is the result?结果是什么?
How does callback know I am referring to f2 ? callback如何知道我指的是f2

There is no result.没有结果。 That code won't even compile.该代码甚至不会编译。

Even if you fix the syntax errors, you never call any functions.即使您修复了语法错误,您也永远不会调用任何函数。

Assuming you fixed all of that, it would know that you were referring to f2 because you would be explicitly passing it as the second argument.假设您修复了所有这些,它会知道您指的是f2因为您将明确地将它作为第二个参数传递。

function my_function_that_accepts_a_callback (word, callback) {
    setTimeout(3000, function() {
            console.log("waiting");
        }
        callback(null, word);
    }
}

function f2(err, result) {
    console.log(result);
}

my_function_that_accepts_a_callback("this is a word", f2);

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

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