Last active
August 29, 2015 13:57
-
-
Save lennybacon/9682841 to your computer and use it in GitHub Desktop.
Chaining asynchronous JavaScript calls with Angular JS
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
angularModule. | |
factory( | |
'callChain', | |
[ | |
function() { | |
return function () { | |
var cs; | |
cs = []; | |
this.add = function (call) { | |
cs.push(call); | |
}; | |
this.execute = function () { | |
var wrap, i; | |
wrap = function (call, callback) { | |
return function () { | |
call(callback); | |
}; | |
}; | |
for (i = cs.length - 1; i > -1; i--) { | |
cs[i] = wrap(cs[i], i < cs.length - 1 ? cs[i + 1] : null); | |
} | |
cs[0](); | |
}; | |
}; | |
} | |
] | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment