Skip to content

Instantly share code, notes, and snippets.

@toboqus
Last active August 29, 2015 14:07
Show Gist options
  • Save toboqus/2fa12706ae4eba9b34f1 to your computer and use it in GitHub Desktop.
Save toboqus/2fa12706ae4eba9b34f1 to your computer and use it in GitHub Desktop.
Synchronously execute functions/load dependencies
$scope.loadDependencies = function(arr){
var deferred = $q.defer();
var arrLen = arr.length,i = -1;
(function loadNext(){
i++;
(i < arrLen) ? arr[i](loadNext) : deferred.resolve();
})();
return deferred.promise;
};
var dep = [
function(next){
setTimeout(function(){
console.log('1');
next();
}, 1000);
},function(next){
setTimeout(function(){
console.log('2');
next();
}, 1000);
},function(next){
setTimeout(function(){
console.log('3');
next();
}, 1000);
}];
$scope.loadDependencies(dep).then(function () {
console.log('finished loading');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment