【问题标题】:NodeJS del module's promise needs explicite function for promise?NodeJS del 模块承诺需要明确的承诺功能吗?
【发布时间】:2017-03-17 23:46:07
【问题描述】:

我不明白为什么这个 del 函数需要一个新函数来实现它的承诺。下面是完整的代码。问题出在 clean 函数上。

gulp.task('styles', ['clean-styles'], function () {
log('Compiling less to css');

return gulp
    .src(config.less)
    .pipe($.less())
    .pipe($.autoprefixer({browsers: ['last 2 version', '> 5%']}))
    .pipe(gulp.dest(config.temp));

});

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

gulp.task('less-watcher', function () {
    gulp.watch([config.less], ['styles']);
});

////////////////

function clean(path, done) {
    log('Cleaning: ' + $.util.colors.blue(path));

    console.log(typeof done); // prints 'function'
    del([path]).then(done); // errors
}

function log(msg) {
    if (typeof msg === 'object') {
        for (var item in msg) {
            if (msg.hasOwnProperty(item)) {
                $.util.log($.util.colors.blue(msg[item]));
            }
        }
    } else {
        $.util.log($.util.colors.blue(msg));
    }
}

这是我得到的错误:

[23:28:32] Using gulpfile ~/WebstormProjects/gulp-tutorial/gulpfile.js
[23:28:32] Starting 'clean-styles'...
[23:28:32] Cleaning: ./.temp**/*.css
function
[23:28:32] 'clean-styles' errored after 36 ms
[23:28:32] Error    
at formatError (/usr/lib/node_modules/gulp/bin/gulp.js:169:10)    
at Gulp.<anonymous> (/usr/lib/node_modules/gulp/bin/gulp.js:195:15)    
at emitOne (events.js:96:13)    
at Gulp.emit (events.js:188:7)    
at Gulp.Orchestrator._emitTaskDone (/home/user/WebstormProjects/gulp-tutorial/node_modules/orchestrator/index.js:264:8)    
at /home/user/WebstormProjects/gulp-tutorial/node_modules/orchestrator/index.js:275:23    
at finish (/home/user/WebstormProjects/gulp-tutorial/node_modules/orchestrator/lib/runTask.js:21:8)    
at cb (/home/user/WebstormProjects/gulp-tutorial/node_modules/orchestrator/lib/runTask.js:29:3)

如果我像下面这样更改“清洁”功能的代码,它奇怪地解决了问题。为什么它拒绝一个功能而接受另一个功能?

function clean(path, done) {
    log('Cleaning: ' + $.util.colors.blue(path));

    console.log(typeof done); // prints 'function'
    del([path]).then(function () {
        done(); // works!
    });
}

即使我使用分配给变量而不是被提升的函数,它仍然有效。那么为什么它不接受承诺中的“完成”功能。这让我大吃一惊。

function clean(path, done) {
    log('Cleaning: ' + $.util.colors.blue(path));

    console.log(typeof done); // prints 'function'
    var test = function () {
        done();
    }
    del([path]).then(test);
}

---------已解决----------

感谢 Johannes Merz 为我回答了这个问题。我认为“完成”功能是我的回调,但我错了。它属于 gulp,所以为了模拟问题检查下面新的 'clean' 功能。

function clean(path, done) {
    log('Cleaning: ' + $.util.colors.blue(path));

    del([path]).then(function (val) {
        console.log(val);
        done('Not null or undefined so this will fail it');
        // done(); but this would work!
    });
}

基本上,promise 会将参数传递给 promise 使用的函数。这搞砸了,因为“完成”回调认为该参数是错误消息或对象。这对我来说很有意义。

done(err);

因此,如果 'err' 为 null 或未定义,我们将没有问题。问题是承诺不知道这一点。承诺是传递一条消息,而不是错误,但 done 函数认为它是一个错误,所以它会出错。

问题解决了:)

【问题讨论】:

    标签: javascript node.js npm gulp del


    【解决方案1】:

    这是因为 del 函数用一个值来解析一个 Promise。看看文档,如果通过 undefined,done 被认为是成功的:

    https://github.com/gulpjs/gulp/blob/master/docs/API.md#accept-a-callback

    因此,如果将 done 直接传递给 then 块,则调用 done('some value that isn't undefined'),这被视为错误,而如果将其包装在额外的函数中,则调用 done();。试试

    del([path]).then(function (delResult) {
        console.log(delResult);
        done(); // works!
    });
    

    查看返回的内容。

    【讨论】:

    • 哇,这令人困惑。我读了这个答案大约 20 次试图理解它。并发和异步问题总是困扰着我。感谢您的帮助!
    猜你喜欢
    • 2017-10-18
    • 1970-01-01
    • 2016-10-25
    • 2019-09-29
    • 1970-01-01
    • 2018-03-31
    • 2016-04-16
    • 2021-03-18
    相关资源
    最近更新 更多