Skip to content

Instantly share code, notes, and snippets.

@lennybacon
Last active August 29, 2015 13:57
Show Gist options
  • Save lennybacon/9682841 to your computer and use it in GitHub Desktop.
Save lennybacon/9682841 to your computer and use it in GitHub Desktop.
Chaining asynchronous JavaScript calls with Angular JS
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