Search code examples
javascriptgulp

gulp 'default' task finishes without running callback


I have recently upgraded from gulp 3 to 4 - I have the upgrade guides, and modified my code to suit, but I am still getting some odd behaviour.

Here is a small reproducible case:

gulp.task('do-nothing', function (done) {
    setTimeout(function () {
        done()
    }, 3 * 1000)
})

gulp.task("default", gulp.parallel("do-nothing"), function (done) {
    console.log(`\n\n\nWHY AM I NOT BEING RUN`)
})

When gulp is run, the output is:

[14:10:57] Using gulpfile ~\Code\certsimple\gulpfile.js
[14:10:57] Starting 'default'...
[14:10:57] Starting 'do-nothing'...
[14:11:00] Finished 'do-nothing' after 3.01 s
[14:11:00] Finished 'default' after 3.02 s

There is no WHY AM I NOT BEING RUN printed to the console.

Why is this?


Solution

  • You have this line:

    gulp.task("default", gulp.parallel("do-nothing"), function (done) {
    

    Note that it has three arguments: the string task name, gulp.parallel(), and an anonymous function call. That is gulp3 syntax. Here is the gulp4 task function signature:

    task([taskName], taskFunction)  // note only **two** arguments.
    

    From gulp 4 documentation for task signature.

    That is why your console.log is never reached. So you just need to put your anonymous function within the gulp.parallel argument to fix it.

    gulp.task("default", gulp.parallel("do-nothing", function (done) {
        console.log(`\n\n\nWHY AM I NOT BEING RUN`)
    }));
    

    Also since you are using gulp.parallel the console.log isn't going to wait for do-nothing to finish.