Node.js模块中的asynchronous函数

我对JavaScript / Node.js很感兴趣,所以对我很感兴趣。 另外我的英语可能不是那么好。

我试图写一个Node.js模块module.js与function,做一些长期运行的工作。 有点像这样:

var exec = require('child_process').exec; module.exports.myFunction1 = function(callback) { // this function runs for like 3 seconds exec('long running shell command' ,function(err,stdout,stderr) { callback(stdout); }) }; module.exports.myFunction2 = function(callback) { // this function runs for like 1 second exec('long running shell command' ,function(err,stdout,stderr) { callback(stdout); }) }; 

现在,我也有一个main.js我调用这些函数:

 var module = require('./module.js'); var output1 = module.myFunction1(); var output2 = module.myFunction2(); 

我的第一个问题是我的函数返回undefined 。 我知道这是因为exec函数asynchronous运行,因此函数在exec完成之前返回。 这基本上是我想要的,但我怎么能告诉我的函数,它应该只有当exec完成时callback?

当我在main.js中调用它们的时候,我也不希望这些函数阻塞node.js。 所以基本上,我的输出上面的代码将是…

 Output myFunction2: Output2 Output myFunction1: Output1 

…因为myFunction2()比myFunction1()快。

我尝试了很多,我在网上find的解决scheme,但似乎没有任何工作正常。

提前非常感谢!

—编辑—

好的,我有一个正确的解决scheme。 现在我的代码如下所示:

module.js

 var Q = require('q'); require('shelljs/global') module.exports = { myFunction1: function () { var deferred = Q.defer(); var result = exec('long running command', {silent:true}).output.toString(); if (ok) { deferred.resolve(result); } else { deferred.reject('Error'); } return deferred.promise; }, myFunction2: function () { var deferred = Q.defer(); var result = exec('long running command', {silent:true}).output.toString(); if (ok) { deferred.resolve(result); } else { deferred.reject('Error'); } return deferred.promise; } } 

现在我的main.js lloks是这样的:

 var module = require('./module'); module.myFunction1() .then(function (result) { console.log('Result 1: ' + result); }) .fail(function (error) { console.log(error) }); module.myFunction2() .then(function (result) { console.log('Result 2: ' + result); }) .fail(function (error) { console.log(error) }); 

我得到了预期的结果:

 Result 1: Output that myFunction1() generated Result 2: Output that myFunction2() generated 

现在我的问题是,myFunction1()总是在myFunction2()之前logging,即使myFunction2()先完成。 我对Promises有什么错误了解吗? 不应该myFunction2()完成后立即返回?

您的function采取callback。 这些参数是在完成时调用的函数,这使得它很容易做到

 var exec = require('child_process').exec; module.exports.myFunction1 = function(callback) { // this function runs for like 3 seconds exec('long running shell command' ,function(err,stdout,stderr) { callback(stdout); }) }; module.myFunction1(function(stdout){ console.log("Output myFunction1: " + stdout); }); 

在你的情况下,使用callback是最简单的解决scheme,但你应该知道还有其他的模式来处理asynchronous执行。 这是一个很好的概述 。 例如,一个受欢迎的解决scheme,特别有趣的是,当你不得不链接asynchronous延续时,就是使用允许的promise

 var exec = require('child_process').exec; module.exports.myFunction1 = function() { return new Promise(function(resolve, fail){ // this function runs for like 3 seconds exec('long running shell command' ,function(err,stdout,stderr) { if (err) fail(err); else resolve(stdout, stderr); }); }); }; module.myFunction1() .then(function(stdout){ console.log("Output myFunction1: " + stdout); }) .then(module.myFunction2) .then(function(stdout){ console.log("Output myFunction2: " + stdout); }) 

起初,我会build议你在你的模块中处理错误( errstderr )。 正如你所看到的,你的函数有一个参数是callback函数。 如果您的asynchronous函数运行,则调用callback函数。 所以你可以像这样使用它:

 module.myFunction1(function(stdout) { console.log("Output myFunction1: " + stdout); module.myFunction2(function(stdout2) { console.log("Output myFunction2: " + stdout2); }); }); 

exec函数也采用callback函数(第一个参数错误err – 首先错误callback)。 还有其他的select,如何处理asynchronous代码的stream量控制(例如库asynchronous )。 您也可以了解Promise ,这是今天错误优先callback的替代scheme。

callback函数不直接返回值…你需要的是设置什么时候会发生值读取。 像这样的东西:

my_function(what_will_happen_when_my_function_will_get_finished());

exectly:

myFunction1(function(data){console.log('hello! I've finished, and received: '+data);});