我需要在grunt.registerTask中调用两个函数,但是第二个函数必须在完成第一个函数之后调用。
因此,我想知道我们是否可以在grunt.registerTask中使用回调、承诺或其他异步机制。
(更具体地说,我需要在第一个函数调用中启动业力,在第二个函数调用中运行业力(以执行初始单元测试)。但是为了运行业力,我需要先启动它。这正是我所缺少的。)
发布于 2015-06-21 16:24:29
我有这样的经历:
grunt.registerTask("name_of_task", ["task_a", "task_b", "task_c"]);"task_b“必须在"task_a”完成后执行。问题是"task_a“是异步的,并且会立即返回,所以我需要一种方法来给"task_a”几秒钟来执行。
解决办法:
grunt.registerTask("name_of_task", ["task_a", "task_b:proxy", "task_c"]);
grunt.registerTask("task_b:proxy", "task_b description", function () {
var done = this.async();
setTimeout(function () {
grunt.task.run("task_b");
done();
}, 2000);
});
};发布于 2015-06-19 11:41:48
来自http://gruntjs.com/creating-tasks
任务可以是异步的。 函数(){ // grunt.registerTask任务进入异步模式,并获取“已完成”函数的句柄。var done = this.async();//运行一些同步内容。Grunt.log.writeln(‘处理任务.’);//和一些异步的东西。setTimeout(函数(){ grunt.log.writeln('All done!');done();},1000);};
发布于 2019-05-29 21:26:53
为了简单..。拥有这个Grunfile.js
grunt.registerTask('a', function () {
let done = this.async();
setTimeout(function () {
console.log("a");
done();
}, 3000);
});
grunt.registerTask('b', function () {
let done = this.async();
console.log("b1");
setTimeout(function () {
console.log("b2");
done();
}, 3000);
console.log("b3");
});
grunt.registerTask('c', function () {
console.log("c");
});
grunt.registerTask("run", ["a", "b", "c"]);然后运行run任务,将产生以下输出
Running "a" task
a
Running "b" task
b1
b3
b2 <-- take a look here
Running "c" task
c
Done.该命令按以下顺序执行:
等待3000毫秒
console.log("a")
console.log("b1")
console.log("b3")
等待3000毫秒
console.log("b2")
console.log("c")
https://stackoverflow.com/questions/30917211
复制相似问题