Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node's del command - callback not firing

Tags:

node.js

gulp

del

I'm working through a pluralsight course on gulp. John Papa is demonstrating how to inject a function that deletes existing css files, into the routine that compiles the new ones.

The callback on the del function is not firing. The del function is running, file are deleted, I see no error messages. If I call the callback manually it executes, so looks like the function is in tact. So I am wondering what would cause del not to want to execute the callback.

delete routine:

function clean(path, done) {
    log('cleaning ' + path);
    del(path, done);   // problem call
}

The 'done' function is not firing, but it does if I change the code to this:

function clean(path, done) {
    log('cleaning ' + path);
    del(path);
    done();
}

Which, of course, defeats the intended purpose of waiting until del is done before continuing on.

Any ideas at to what's going on would be appreciated.

for reference (in case relevant):

compile css function:

gulp.task('styles', ['clean-styles'], function(){
    log('compiling less');
    return gulp
        .src(config.less)
        .pipe($.less())
        .pipe($.autoprefixer({browsers:['last 2 versions', '> 5%']}))
        .pipe(gulp.dest(config.temp));
});

injected clean function:

gulp.task('clean-styles', function(done){
    var files = config.temp + '/**/*.css';
    clean(files, done);
});

UPDATE

If anyone else runs into this, re-watched the training video and it was using v1.1 of del. I checked and I was using 2.x. After installing v 1.1 all works.

like image 286
Steve Avatar asked Sep 24 '15 21:09

Steve


2 Answers

del isn't a Node's command, it's probably this npm package. If that's the case it doesn't receive a callback as second parameter, instead it returns a promise and you should call .then(done) to get it called after the del finishes.

Update

A better solution is to embrace the Gulp's promise nature:

Change your clean function to:

function clean(path) {
  return del(path); // returns a promise
}

And your clean-styles task to:

gulp.task('clean-styles', function(){
  var files = config.temp + '/**/*.css';
  return clean(files);
});
like image 118
gfpacheco Avatar answered Sep 28 '22 17:09

gfpacheco


As of version 2.0, del's API changed to use promises.

Thus to specify callback you should use .then():

del('unicorn.png').then(callback);

In case you need to call it from a gulp task - just return a promise from the task:

gulp.task('clean', function () {
  return del('unicorn.png');
});
like image 40
Vitaliy Ulantikov Avatar answered Sep 28 '22 18:09

Vitaliy Ulantikov