鉴于以下gulp任务,为什么我会收到以下错误?
错误:任务完成回调调用次数过多
function myTask(options, cb) { // cb is the gulp cb
var serverInstance = http.createServer(dispatch({ /*routes*/ }));
serverInstance.listen(options.port, function() {
cb(); // Stack trace identifies this line as throwing the error
});
}
function partial(fn) {
var args = Array.prototype.slice.call(arguments, 1);
return function() {
return fn.apply(this, args.concat(Array.prototype.slice.call(arguments)));
};
}
gulp.task('task-name', ['task-dependency'], partial(myTask, { port: 8080 }));
Run Code Online (Sandbox Code Playgroud)
编辑:
以下修改使其工作(但我的问题仍然存在):
gulp.task('task-name', ['task-dependency'], function(cb) {
partial(myTask, { port: 8080 })(cb);
});
Run Code Online (Sandbox Code Playgroud)
这是因为gulp使用启发式(包括回调的返回值以及是否接受回调参数)来检测异步vs同步任务,并且它会相应地对待它们.我的部分函数返回一个没有声明参数的函数,当它异步时,它会认为它是同步的.
修改partial以使用单个命名参数返回函数解决了该问题.
//...
return function(cb) {
return fn.apply(this, args.concat(slice.call(arguments)));
};
//...
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3676 次 |
| 最近记录: |