Posts

Showing posts with the label Q

Angularjs $q.all

Answer : In javascript there are no block-level scopes only function-level scopes : Read this article about javaScript Scoping and Hoisting. See how I debugged your code: var deferred = $q.defer(); deferred.count = i; console.log(deferred.count); // 0,1,2,3,4,5 --< all deferred objects // some code .success(function(data){ console.log(deferred.count); // 5,5,5,5,5,5 --< only the last deferred object deferred.resolve(data); }) When you write var deferred= $q.defer(); inside a for loop it's hoisted to the top of the function, it means that javascript declares this variable on the function scope outside of the for loop . With each loop, the last deferred is overriding the previous one, there is no block-level scope to save a reference to that object. When asynchronous callbacks (success / error) are invoked, they reference only the last deferred object and only it gets resolved, so $q.all is never resolved because it still waits for other deferred ob...