Skip to content

Instantly share code, notes, and snippets.

@wliyongfeng
Last active October 2, 2015 02:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wliyongfeng/9408480 to your computer and use it in GitHub Desktop.
Save wliyongfeng/9408480 to your computer and use it in GitHub Desktop.
recursive call of Deferred methods
// 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);
@wliyongfeng
Copy link
Author

jsFiddle, run and check the console.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment