You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Gulp中series/parallel功能正常却提示未标记异步完成的问题

Gulp 4.x 任务异步完成警告问题及解决方法

问题详情

我按照Gulp官方文档中series()和parallel()的说明及示例编写代码,但直接返回gulp.parallel()调用的方式完全无效。以下是我的代码:

import gulp from "gulp";

export const test = () => {
    return gulp.parallel(
        async () => console.log("Async done"),
        cb => { console.log("cb done"); cb(); },
    )(); // 不调用函数()则无任何执行
};

package.json中的Gulp版本:

{
  "devDependencies": {
    "gulp": "^4.0.2",
    // ...
  }
  // ...
}

代码功能正常,能看到任务输出,但每次都会弹出警告:

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

我已经尝试过:

  • 完全按照示例返回gulp.parallel()不执行,此时没有任何任务执行;
  • 移除async方法或回调方法(只保留单个任务),警告依然存在。

另外,我无法像多数文档示例那样用以下方式导入:

import gulp, { series, parallel } from "gulp";

会报错:

Named export 'parallel' not found. The requested module 'gulp' is a CommonJS module, which may not support all module.exports as named exports.

可行解决方法

方法1:给任务函数添加async关键字

给test函数添加async关键字后,警告消失。推测原因是Gulp会处理返回的Promise,无需在parallel结果上使用await(parallel本身也不是Promise)。修改后代码:

export const test = async () => { // 添加async
    return gulp.parallel(
        async () => console.log("Async done"),
        cb => { console.log("cb done"); cb(); },
    )();
};

方法2:用series包裹并手动触发回调

将所有任务包裹在另一个series中,最后添加触发回调的步骤。注意这种写法下,test会在最终任务完成前被标记为已完成。代码如下:

export const test = (cb) => {
    return gulp.series(
        gulp.parallel(
            async () => { await new Promise(r => setTimeout(r, 1000)); console.log("Async done") },
            cb => { console.log("cb done"); cb(); },
        ),
        async () => cb(), // 此步骤在test完成后执行
    )();
}

执行输出:

...
[12:24:44] Finished 'test' after 1.01 s
[12:24:44] Finished '' after 1.33 ms

内容的提问来源于stack exchange,提问作者Luke Vo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 19:22:31