gpt4 book ai didi

javascript - "Close"spawn() 函数的回调在 grunt 插件中不起作用

转载 作者:行者123 更新时间:2023-11-30 05:42:34 24 4
gpt4 key购买 nike

我正在创建一个 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");
});
};

然而,当我尝试执行这个垃圾任务时,console.log("FINISH!FINISH!FINISH!"); 永远不会被 mi 回调执行...有人可以提供帮助我用这个?

最佳答案

最后我找到了这个问题的解决方案(Wait async grunt task to finish)。解决这个问题的关键是在注册中使用函数this.async。一个可能的解决方案是:

'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");
});
};

关于javascript - "Close"spawn() 函数的回调在 grunt 插件中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19909883/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com