使用 deferred/promise 实现某些异步函数的超时是一种常见的模式:
// Create a Deferred and return its Promise
function timeout(funct, args, time) {
var dfd = new jQuery.Deferred();
// execute asynchronous code
funct.apply(null, args);
// When the asynchronous code is completed, resolve the Deferred:
dfd.resolve('success');
setTimeout(function() {
dfd.reject('sorry');
}, time);
return dfd.promise();
}
现在我们可以执行一些名为的异步函数myFunc
并处理超时:
// Attach a done and fail handler for the asyncEvent
$.when( timeout(myFunc, [some_args], 1000) ).then(
function(status) {
alert( status + ', things are going well' );
},
function(status) {
alert( status + ', you fail this time' );
}
);
好吧,让我们来扭转这个故事吧!想象一下myFunc
本身返回一个承诺(注意:承诺不延迟,我无法更改它):
function myFunc(){
var dfd = new jQuery.Deffered();
superImportantLibrary.doSomething(function(data)){
if(data.length < 5){
dfd.reject('too few data');
}
else{
dfd.resolve('success!');
}
}, {'error_callback': function(){
dfd.reject("there was something wrong but it wasn't timeout");}
}});
return dfd.promise();
}
现在如果我包装myFunc
in timeout
,我将失去处理与超时不同的错误的能力。如果myFunc
发出进度事件,我也会失去它。
那么问题来了:如何修改timeout
函数以便它可以接受返回承诺的函数而不丢失其错误/进度信息?