Last active
October 2, 2015 02:59
-
-
Save wliyongfeng/9408480 to your computer and use it in GitHub Desktop.
recursive call of Deferred methods
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// test methods fun1 and fun2 for test | |
var fun1 = function () { | |
var deferred = $.Deferred(); | |
setTimeout(function () { | |
console.log('I am fun1'); | |
deferred.resolve(); | |
}, 1000); | |
return deferred.promise(); | |
} | |
var fun2 = function () { | |
var deferred = $.Deferred(); | |
setTimeout(function () { | |
console.log('I am fun2'); | |
deferred.resolve(); | |
}, 1000); | |
return deferred.promise(); | |
} | |
// make test jQuery asynchronous methods array | |
var arr = [fun1, fun2]; | |
// This the target method for implementing that call jQuery asyn methods recursively | |
var recursive = function(i, arr) { | |
return execute(arr[i]).then(function() { | |
if (i + 1 < arr.length) { | |
recursive(i + 1, arr); | |
} | |
}); | |
function execute(func) { | |
return func(); | |
} | |
} | |
// run our test | |
recursive(0, arr); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
jsFiddle, run and check the console.