将gullow回调直接传递给promise时出现异常
Exception when passing gulp callback directly to promise
我已经将gump任务"clean code"和函数"clean"定义为
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)
但是,当我以以下方式重构函数"clean"时,一切正常
function clean (path, done) {
var f = function () {
done();
};
del(path).then(f);
}
我不明白区别在哪里,也不明白为什么用f包装会使任务在中工作
假设您使用的是这个库,那么为del
函数返回的Promise实际上返回了一个参数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函数会将其解释为导致应用程序崩溃的错误参数。通过自己调用done()
,不会转发任何参数,并且任务将正确执行。
相关文章:
- 使用promise和mongoose对文档进行排序
- 测试Angular Service解决错误回调中的promise
- 节点协同与生成器和Promise并行流量控制
- 根据是否解析了 Promise 从函数返回值
- 将一个方法转换为promise:Nodejs
- 在javaservlet doPost方法中启动线程时,无法返回异常消息
- 函数在promise被解析后被调用,但Jasmine未通过测试.为什么?
- document.applet.method在Mounatin Lion上抛出safari 6+JDK7异常
- js promise没有正确关闭
- Angularjs使用“;这个“;promise内的关键字回调
- NodeJS和pg promise,捕获PostgreSQL异常
- 将gullow回调直接传递给promise时出现异常
- 使用Q.promise时处理异常的最佳实践
- Angular Service在返回promise时抛出异常
- 为什么,如果“$q.all”没有返回一个promise数组,则是一个未抛出的异常
- node.js Q promise不会通过拒绝处理程序传播异常
- 在JavaScript Promise中引发异步失败的异常
- 使用bluebird promise进行异步异常处理
- Angular-在promise链的第一个函数中处理$q中的异常
- 在Javascript中使用promise时,如何在出现异常后重试