{ return gulp.src("**/*.scss") .p...">

How to use async / await using Gulp 4?

I am trying to do something like this:

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

(print gulp-print )

But he 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

i.e. It ends before messages are printed.

I am using Gulp 4 (alpha 2, I think) and TypeScript (1.8.0-dev.20151204).

The generated (ES6) code is as follows:

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

Where __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 this possible to work? I want to use awaittasks in my function, but I cannot do this without marking the function as asynchronous.

I'm probably missing something obvious.

+4
source share
1 answer

.

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

finish end (. ).

, , , - :

  • Gulp async, , promises, .
  • , end finish, , .

, , , resume(), on("end"), , finish. , . , .

, , , .

: , .

+2

Source: https://habr.com/ru/post/1619254/


All Articles