节点的 del 命令 - 回调未触发 [英] Node's del command - callback not firing

查看:11
本文介绍了节点的 del 命令 - 回调未触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习关于 gulp 的复数课程.John Papa 正在演示如何将删除现有 css 文件的函数注入到编译新 css 文件的例程中.

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.

del 函数的回调未触发.del 函数正在运行,文件被删除,我没有看到任何错误消息.如果我手动调用回调它会执行,所以看起来该函数是完整的.所以我想知道是什么导致 del 不想执行回调.

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.

删除例程:

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();
}

当然,这违背了等待 del 完成后再继续的预期目的.

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.

供参考(如果相关):

编译css函数:

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));
});

注入清洁功能:

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

更新

如果其他人遇到这种情况,请重新观看培训视频,它使用的是 v1.1 的 del.我检查了一下,我使用的是 2.x.安装 v 1.1 后一切正常.

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.

推荐答案

del 不是 Node 的命令,可能是 这个 npm 包.如果是这种情况,它不会收到回调作为第二个参数,而是返回一个承诺,您应该调用 .then(done) 以在 del 之后调用它完成.

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.

更新

更好的解决方案是接受 Gulp 的承诺性质:

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

将您的 clean 函数更改为:

Change your clean function to:

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

你的 clean-styles 任务:

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

这篇关于节点的 del 命令 - 回调未触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆