Search code examples
gulptaskdel

Gulp: Why my 'clean' task don't work using the callback function?


My gulp file is above:

var gulp = require('gulp');

var concat = require('gulp-concat');
var uglify = require('gulp-uglify');
var del = require('del');

gulp.task('clean', function(cb) {
    del(['dist/*.js'], cb);
});

When I run 'gulp clean', my output is:

[12:45:05] Using gulpfile

[12:45:05] Starting 'clean'...

The command delete the files of dist folder.

If I remove the callback function, the result is:

gulp.task('clean', function() {

    del(['dist/*.js']);

});

[12:54:07] Using gulpfile 

[12:54:07] Starting 'clean'...

[12:54:07] Finished 'clean' after 4.

What I'm doing wrong with the callback function?

Regards.


Solution

  • del by itself returns a promise, so if you'd like to use it with a callback you would have to do something like the following:

    del(['dist/*.js']).then(function(paths) {
        cb(paths);
    });
    

    Or:

    del(['dist/*.js']).then(cb);
    

    paths being an array of deleted folders/files.

    There is also del.sync, which doesn't return a promise but an array of deleted folders/files.