我定义了“清洁代码”任务和函数“干净”任务如下
gulp.task('clean-code', function (done) {
var files = ...;
clean(files, done);
});
function clean (path, done) {
del(path).then(done);
}得到了错误
/usr/local/bin/node /usr/local/lib/node_modules/gulp/bin/gulp.js --color --gulpfile /Users/[path to project]/Gulpfile.js clean-code
[11:45:04] Using gulpfile /Users/[path to project]/Gulpfile.js
[11:45:04] Starting 'clean-code'...
[11:45:04] Cleaning: ./.tmp/**/*.js,./build/**/*.html,./build/js/**/*.js
[11:45:04] 'clean-code' errored after 8.68 ms
[11:45:04] Error
at formatError (/usr/local/lib/node_modules/gulp/bin/gulp.js:169:10)
at Gulp.<anonymous> (/usr/local/lib/node_modules/gulp/bin/gulp.js:195:15)
at emitOne (events.js:77:13)
at Gulp.emit (events.js:169:7)
at Gulp.Orchestrator._emitTaskDone (/Users/[path to project]/node_modules/gulp/node_modules/orchestrator/index.js:264:8)
at /Users/[path to project]/node_modules/gulp/node_modules/orchestrator/index.js:275:23
at finish (/Users/[path to project]/node_modules/gulp/node_modules/orchestrator/lib/runTask.js:21:8)
at cb (/Users/[path to project]/node_modules/gulp/node_modules/orchestrator/lib/runTask.js:29:3)但是当我用下面的方式重构函数‘干净’时,一切都好了
function clean (path, done) {
var f = function () {
done();
};
del(path).then(f);
}我不明白这有什么区别,为什么用f包装能使任务工作
发布于 2016-01-03 11:18:20
假设您使用的是这库,那么del函数返回的承诺实际上会返回一个参数paths,这是不值得的。您可以验证是否存在这样的论点:
function clean (path, done) {
del(path).then(function(paths) {
console.log(paths);
done();
});
}在您的代码中:
function clean (path, done) {
del(path).then(done);
}将paths参数转发给done函数,后者将将其解释为导致应用程序崩溃的错误参数。通过自己调用done(),不会转发任何参数,任务将被正确执行。
https://stackoverflow.com/questions/34575809
复制相似问题