[英]Does nodejs create background task or child process to execute callback function
[英]nodejs grunt child process callback function example
您是否可以帮助以下使用grunt运行的node exec命令示例?
echo
命令正在执行,并且创建了hello-world.txt
,但回调函数中的grunt.log.writeln
命令未触发。
var exec = require('child_process').exec,
child;
child = exec('echo hello, world! > hello-world.txt',
function(error, stdout, stderr){
grunt.log.writeln('stdout: ' + stdout);
grunt.log.writeln('stderr: ' + stderr);
if (error !== null) {
grunt.log.writeln('exec error: ' + error);
}
}
);
参考文献:
http://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options
DOH! 这是在FAQ中。
将Gruntjs用于异步任务时,必须手动指定任务何时完成。 https://github.com/gruntjs/grunt/wiki/Frequently-Asked-Questions
https://github.com/robdodson/async-grunt-tasks
https://github.com/rwldrn/dmv/blob/master/node_modules/grunt/docs/api_task.md
对于后代,上面应该是这样的:
var exec = require('child_process').exec,
child,
done = grunt.task.current.async(); // Tells Grunt that an async task is complete
child = exec('echo hello, world! > hello-world.txt',
function(error, stdout, stderr){
grunt.log.writeln('stdout: ' + stdout);
grunt.log.writeln('stderr: ' + stderr);
done(error); // Technique recommended on #grunt IRC channel. Tell Grunt asych function is finished. Pass error for logging; if operation completes successfully error will be null
}
}
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.