Sla*_*waj 1 javascript asynccallback meteor
我尝试从Meteor环境中的child_process命令获取结果.似乎child_process中有一些特殊的东西,我不明白.这是我用于测试的代码
Meteor.startup(function () {
exec = Npm.require('child_process').exec;
});
function bind_environment_callback(error) {
console.log('Error binding environment for a callback', error.stack);
}
function get_git_commit_hash(cb) {
exec(
'git rev-parse HEAD',
Meteor.bindEnvironment(
function(error, stdout, stderr) {
if (error) {
cb('Error retrieving commit hash', null);
} else {
console.log("Inside get_git_commit_hash:" + stdout.slice(0,stdout-1).toString());
cb(null, stdout.slice(0,stdout-1).toString());
}
},
bind_environment_callback
)
);
}
function dummy(cb){
setTimeout(function(){
cb(null, 'Dummy result');
},
100);
}
Meteor.methods({
test: function() {
var get_git_commit_hash_sync = Meteor._wrapAsync(get_git_commit_hash);
var result= get_git_commit_hash_sync();
console.log('Call 1:' + result);
var dummy_sync = Meteor._wrapAsync(dummy);
result= dummy_sync();
console.log('Call 2:' + result);
}
});
Run Code Online (Sandbox Code Playgroud)
当我Meteor.call('test')在浏览器中运行时,我在控制台中看到以下输出:
Inside get_git_commit_hash:d53ffc7f5db26c6e2b40bfcce7a1e2e0d6610ece
Call 1:
Call 2:Dummy result
Run Code Online (Sandbox Code Playgroud)
任何人都可以帮我理解为什么我没有在第一次通话中得到结果?
我不确定这是否有效,但试一试:
runCmd = Meteor.wrapAsync(exec)
var result = runCmd("git rev-parse HEAD");
console.log(result);
Run Code Online (Sandbox Code Playgroud)
然后你可以处理结果.还要注意你已经完成了stdout-1,它应该总是返回NaN你正在运行的命令,而不是数字.
更新
Meteor._wrapAsync现在是Meteor.wrapAsync
| 归档时间: |
|
| 查看次数: |
1045 次 |
| 最近记录: |