gulp: Did you forget to signal async completion?处理

gulp: Did you forget to signal async completion?处理新思路
背景
学习gulp的前端自动化构建,按照示例代码,跑了一个简单的task,控制台打出如下提示:

The following tasks did not complete: testGulp 
Did you forget to signal async completion?

查阅Stack Overflow,表示有五种解决办法,在这里提供一种更简单的改法。

问题重现
原代码:

const gulp = require('gulp');
gulp.task('testGulp', () => {
    console.log('Hello World!');
});

控制台报错:

The following tasks did not complete: testGulp 
Did you forget to signal async completion?

解决方法,使用 async 和 await。

修改后代码

const gulp = require('gulp');
gulp.task('testGulp', async() => {
   await console.log('Hello World!');
});

控制台输出正常
[13:18:37] Starting ‘testGulp’…
Hello World!
[13:18:37] Finished ‘testGulp’ after 2.77 ms

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

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

done回调函数的作用是在task完成时通知Gulp(而不是返回一个流),而task里的所有其他功能都纯粹依赖Node来实现。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值