0
我定義了一口任務「乾淨代碼」和功能「乾淨」,如下異常傳遞一飲而盡回調時,直接答應
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)
但是,當我ve重構函數'乾淨'在以下方式一切正常
function clean (path, done) {
var f = function() {
done();
};
del(path).then(f);
}
我不明白差異在哪裏和爲什麼使用f完成包裝會使任務正常工作
如果我不得不猜測 - 'done'帶有參數 - 第一個參數是一個'Error'來表示突然完成。這是節點回調約定。如果你在'then'之後調用'done'並且'del'有一個返回值 - 它會失敗。如果你使用藍鳥承諾,你可以使用del(path).nodeify(done)來克服這個問題。 –