我正在编写几个函数,它们是有效的延迟对象,它们依赖于其他延迟对象的不同组合。
function takesOneSecond() {
return $.Deferred(function(deferred) {
// Does something...
}).promise();
}
function takesOneMinute() {
return $.Deferred(function(deferred) {
// Does something...
}).promise();
}
function takesThreeMinutes() {
return $.Deferred(function(deferred) {
// Does something...
}).promise();
}
function mySwitchingFunction() {
return $.Deferred(function(deferred) {
// Does something here..
// Effectively chooses one of several other functions to call.
if(/* choose 1 second */) {
// We tie ourselves to the '1 second' function.
// Call that function.
takesOneSecond().done(function() {
deferred.resolve(); // If that's done, I'm done too.
}).fail(function() {
deferred.reject(); // If that failed, I've failed too.
});
} else if(/* choose 1 minute */) {
// Etc..
} else if(/* choose 3 minutes */) {
// Etc..
}
}).promise();
}
我写了很多这段代码,没有其他方法可以制作延迟镜像或级联另一个延迟的相同“已解决”或“拒绝”状态吗?
takesOneSecond().done(function() {
deferred.resolve(); // If that's done, I'm done too.
}).fail(function() {
deferred.reject(); // If that failed, I've failed too.
});