我相信你的問題是這個任務:
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? –