0

例如,我的功能是这样的

function summ(a,b)
{
 return a+b;
}

module.exports.summ = summ;

在其他文件中:

var obj = require('./that file')

function app()
{
 var s = obj.summ(7,7);
}

如果我console.log(s); 说它给出了完美的答案。我怀疑这是否会在请求频繁出现时一直出现,因为我在休息 api 中使用这种返回?或者需要回调函数,结果是

function summ(a,b,callback)
{
 callback(a+b);
}

function app()
{
 obj.summ(7,7,function(result){
 var s = result;
  }
 }
4

1 回答 1

0

As long as your summ function behaves in a synchronous manner, like it does in your example code, you don't have to use callbacks.

If you would use asynchronous functions in summ (anything I/O related, like reading a file, querying a database, opening a network connection), that would require passing a callback function because you would have to wait until the action is completed before you return the result (by calling the callback function).

于 2013-04-16T17:28:57.430 回答