简体   繁体   English

如何使用Gulp 4 async / await?

[英]How to use async/await with Gulp 4?

I'm trying to do something like this: 我正在尝试做这样的事情:

gulp.task("test", async () => {
    return gulp.src("**/*.scss")
        .pipe(print((filePath) => `File: ${filePath}`));
});

(print is gulp-print ) (打印是gulp-print

But it gives the following: 但它给出了以下内容:

[22:08:43] Starting 'test'...
[22:08:43] Finished 'test' after 12 ms
[22:08:43] File: src\app\styles\app.scss
[22:08:43] File: src\app\styles\test.scss

ie It finishes before the messages are printed. 即它在打印消息之前完成。

I'm using Gulp 4 (alpha 2 I think) and TypeScript (1.8.0-dev.20151204). 我正在使用Gulp 4(我认为是alpha 2)和TypeScript(1.8.0-dev.20151204)。

The generated (ES6) code looks like this: 生成的(ES6)代码如下所示:

gulp.task("test", () => __awaiter(this, void 0, Promise, function* () {
    return gulp.src("**/*.scss")
        .pipe(print((filePath) => `File: ${filePath}`));
}));

Where __awaiter is: __awaiter是:

var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promise, generator) {
    return new Promise(function (resolve, reject) {
        generator = generator.call(thisArg, _arguments);
        function cast(value) { return value instanceof Promise && value.constructor === Promise ? value : new Promise(function (resolve) { resolve(value); }); }
        function onfulfill(value) { try { step("next", value); } catch (e) { reject(e); } }
        function onreject(value) { try { step("throw", value); } catch (e) { reject(e); } }
        function step(verb, value) {
            var result = generator[verb](value);
            result.done ? resolve(result.value) : cast(result.value).then(onfulfill, onreject);
        }
        step("next", void 0);
    });
};

Is it possible to get this working? 有可能让这个工作吗? I want to use await within my task function, but I can't do this without marking the function as asynchronous. 我想在我的任务函数中使用await ,但是如果不将函数标记为异步,我就不能这样做。

I'm probably missing something obvious. 我可能错过了一些明显的东西。

I was able to get this working with help from here and here . 我能够在这里这里得到帮助。

gulp.task("test", async () => {
    await new Promise((resolve, reject) => {
        gulp.src("**/*.scss")
            .pipe(print((filePath) => `File: ${filePath}`))
            .on("end", resolve);
    });
});

In my real task, I had to use the finish event instead of the end event (see the 2nd link above). 在我的实际任务中,我不得不使用finish事件而不是end事件(参见上面的第二个链接)。

It's not crystal-clear how this all works, but I think it's something along the lines of: 这一切都运作起来并不是很清楚,但我认为这有点像:

  1. Gulp functions are async, so they need to be awaited, but since they aren't promises, you have to wrap them in one. Gulp函数是异步的,因此需要等待它们,但由于它们不是承诺,因此必须将它们包装在一起。
  2. In order to resolve the promise, you need to listen to either the end or the finish event, and which one depends on what the last pipe returns. 为了解决这个问题,你需要监听endfinish事件,以及哪一个取决于最后一个管道返回的内容。

Another option that appears to work is resume() followed by on("end") , in cases where finish is usually needed. 在通常需要finish情况下,似乎有效的另一个选项是resume()后跟on("end") This is probably because of this . 这可能是因为这个原因 I'm not sure which option is better. 我不确定哪个选项更好。

I would love to understand this more, so if you're able to explain this in simple terms, please feel free to comment or post an answer. 我希望更多地了解这一点,所以如果您能够用简单的术语解释这一点,请随时发表评论或发布答案。

Edit: You can also return the promise, instead of awaiting it. 编辑:您也可以返回承诺,而不是等待它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM