我相信你的问题是这个任务:
grunt.registerTask('prepare-dist', 'Creates folders needed for distribution', function() {
var folders = ['dist/css/images', 'dist/imgs/icons'];
for (var i in folders) {
var done = this.async();
grunt.util.spawn({ cmd: 'mkdir', args: ['-p', folders[i]] }, function(e, result) {
grunt.log.writeln('Folder created');
done();
});
}
});
如果你有多个文件夹,无论是异步()和()完成将被多次调用。异步是作为一个简单的标志(true/false)实现的,并且被调用一次。第一次完成()调用允许任何后续任务运行。
有很多方法可以将调用移动到异步并完成循环。快速谷歌搜索如:nodejs how to callback when a series of async tasks are complete
会给你一些额外的选择。一对夫妇的快速(&脏)的例子:
// Using a stack
(function() {
var work = ['1','2','3','4','5']
function loop(job) {
// Do some work here
setTimeout(function() {
console.log("work done");
work.length ? loop(work.shift()) : done();
}, 500);
}
loop(work.shift());
function done() {
console.log('all done');
}
})();
- 或 -
// Using a counter (in an object reference)
(function() {
var counter = { num: 5 }
function loop() {
// Do some work here
setTimeout(function() {
--counter.num;
console.log("work done");
counter.num ? loop() : done();
}, 500);
}
loop();
function done() {
console.log('all done');
}
})();
来源
2013-05-20 03:05:39
dc5
我从来没有与步兵的this.async问题()。可能是另一项任务的不良副作用?你有没有尝试你的任务链没有imagemin? –