spawn()函数的“closures”callback在grunt插件中不起作用

我正在创build一个grunt插件,它广泛使用了grunt.util.spawn函数( http://gruntjs.com/api/grunt.util#grunt.util.spawn )。 我的插件的基本框架如下:

 'use strict'; module.exports = function (grunt) { var spawn = require("child_process").spawn; var createCommit = function (text) { var commit = grunt.util.spawn({ cmd: "git", args: ["commit","-a","-m", text] }, function() { console.log("FINISH! FINISH! FINISH!"); }); }; grunt.registerMultiTask("myplugin", "Plugin to commit awesome things", function () { createCommit("0.2.0"); }); }; 

不过,当我试图执行这个grunk任务时, console.log("FINISH! FINISH! FINISH!"); 永远不会执行英寸callback…有人可以帮助我呢?

最后,我在这个问题中find了一个解决scheme( 等待asynchronous咕task任务完成 )。 解决这个问题的关键是使用注册里面的this.async函数。 可能的解决scheme可能是:

 'use strict'; module.exports = function (grunt) { var spawn = require("child_process").spawn, donePromise; var createCommit = function (text) { var i = 0; var commit = grunt.util.spawn({ cmd: "git", args: ["commit","-a","-m", text] }, function() { callback(); donePromise(); }); }; grunt.registerMultiTask("myplugin", "Plugin to commit awesome things", function () { donePromise = this.async(); createCommit("0.3.0"); }); };