为什么我会得到“任务完成回调太多次”在吞咽? [英] Why might I be getting "task completion callback called too many times" in gulp?
问题描述
$ b
错误:任务完成回调调用次数太多
函数myTask(options,cb){// cb是吞噬cb
var serverInstance = http.createServer(dispatch({/ * routes * /}));
serverInstance.listen(options.port,function(){
cb(); //堆栈跟踪标识此行为抛出错误
});
函数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}));
编辑:
gulp.task('task-name' ,['task-dependency'],function(cb){
partial(myTask,{port:8080})(cb);
});
这是因为gulp使用启发式(包括返回值的回调以及它是否接受回调参数)来检测异步与同步任务,并且相应地对它们进行不同的处理。我的部分函数返回一个没有声明参数的函数,它会欺骗吞咽者,认为它是异步时同步的。
修改 partial $> c $ c>返回带有单个命名参数的函数解决了这个问题。
// ...
返回函数(cb){
返回fn.apply(this,args.concat(slice.call(arguments)));
};
// ...
Given the following gulp task, why might I be getting the following error?
Error: task completion callback called too many times
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 }));
Edit:
The following modification makes it work (my question still remains however):
gulp.task('task-name', ['task-dependency'], function(cb) {
partial(myTask, { port: 8080 })(cb);
});
This is because gulp uses heuristics (including the return value of the callback and whether it accepts a callback parameter) to detect async vs sync tasks, and it treats them differently accordingly. My partial function returns a function with no parameters declared that was tricking gulp into thinking it was synchronous when it was asynchronous.
Modifying partial
to return a function with a single named parameter solved the issue.
//...
return function(cb) {
return fn.apply(this, args.concat(slice.call(arguments)));
};
//...
这篇关于为什么我会得到“任务完成回调太多次”在吞咽?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!