gulp报错:The following tasks did not complete: task_name

问题描述

在如下代码运行时报错:

gulp.task('A' , function(){
   console.log('A') 
});
gulp.task('B' , function(){ //运行B之前先去运行A
   console.log('B')
});

gulp.task('my-task', gulp.series("A","B",function(){
	console.log("AB")
}))

报错如下:

D:\me\gulp\gulp-test>gulp task
[14:36:02] Using gulpfile D:\me\gulp\gulp-test\gulpfile.js
[14:36:02] Starting 'task'...
[14:36:02] Starting 'A'...
A
[14:36:02] The following tasks did not complete: task, A
[14:36:02] Did you forget to signal async completion?

分析问题

在不使用文件流的情况下,向task的函数里传入一个名叫cb的回调函数,以结束task,如下代码所示:

gulp.task('test', cb => {
  console.log('Hello World!');
  cb();
});

解决问题

  • 添加asyncawait异步方法处理:
gulp.task('A' , async function(){
  await console.log('A') 
});
gulp.task('B' , async function(){ 
   await console.log('B')
});

gulp.task('my-task', gulp.series("A","B", async function(){
	await console.log("AB")
}))
  • 添加结束回调done()
gulp.task('A' , function(){
  console.log('A') 
  done()
});
gulp.task('B' , function(done){ 
   console.log('B')
   done()
});

gulp.task('my-task', gulp.series("A","B", function(done){
	console.log("AB")
	done()
}))
  • 最终打印结果
[15:00:29] Using gulpfile D:\me\gulp\gulp-test\gulpfile.js
[15:00:29] Starting 'my-task'...
[15:00:29] Starting 'A'...
A
[15:00:29] Finished 'A' after 7.08 ms
[15:00:29] Starting 'B'...
B
[15:00:29] Finished 'B' after 2.26 ms
[15:00:29] Starting ''...
AB
[15:00:29] Finished '' after 2.02 ms
[15:00:29] Finished 'my-task' after 20 ms

你可能感兴趣的:(node,前端工具,javascript)