An example of doing a deferred FOR loop in AngularJS, which is handy if you wish to execute promises sequentially inside your loop, and only move to the next iteration after your promise is resolved.
I have a list of objects that are passed to a deferred function. I want to call the function with the next object only after the previous call is resolved. Is there any way I can do this?
You can't use .forEach() if you want to wait for a promise. Javascript and promises just don't work that way.
You can iterate manually and advance the iteration only when the previous promise finishes.
When using a FOR, the loop continues to execute without waiting for whatever deferred functions need to execute inside the loop.
Same problem as above.
If you simply want to know when all of your promises are resolved, then that's fine. The $q.all() executes all promises simultaneously/in parallel and resolves once they have all finished.
If you want to execute promises SEQUENTIALLY, this example is exactly what you need