nodejs gruntsubprocesscallback函数的例子
你会好心帮助下面一个节点执行命令运行与咕噜的例子?
echo
命令正在执行,并且创build了hello-world.txt
,但callback函数中的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
从节点subprocess中检索一个值
DOH! 这是常见问题。
在使用Gruntjs进行asynchronous任务时,必须手动指定任务何时完成。 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 } } );
- 节点 – 等待循环完成?
- NodeJS V8将附加parameter passing给callback函数
- 找回内部callback的另一个发现(…),如何逃离callback地狱?
- 将variables传递到Node.js中的callback函数的最佳方法
- Api数据与Node.jsasynchronous并检索数据使其同步
- 使用嵌套的async.forEachSeries和async.waterfall来控制Lambda Node.js代码的sorting
- 如何使用函数内的callback函数?
- 自动包装任何node.jscallback
- Node.jscallback混乱 – MongoDB的findOne()的第三个参数:为什么这是允许的?