Last active
August 29, 2015 14:07
-
-
Save toboqus/2fa12706ae4eba9b34f1 to your computer and use it in GitHub Desktop.
Synchronously execute functions/load dependencies
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
$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; | |
}; |
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
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