做到这一点的“良好的node.js /事件驱动”方式是 不等待 。
与使用节点等事件驱动的系统时,几乎所有其他内容一样,您的函数应接受一个回调参数,该参数将在计算完成时被调用。调用者不应等待正常意义上的“返回”值,而应发送将处理结果值的例程:
function(query, callback) { myApi.exec('SomeCommand', function(response) { // other stuff here... // bla bla.. callback(response); // this will "return" your value to the original caller });}所以你不要这样使用它:
var returnValue = myFunction(query);
但是像这样:
myFunction(query, function(returnValue) { // use the return value here instead of like a regular (non-evented) return value});


