Skip to content

An example of doing a deferred FOR loop in AngularJS, which is handy if you have to finish function execution inside the for loop sequentially, before going to the next iteration of a loop.

Notifications You must be signed in to change notification settings

pietervw/Deferred-Angular-FOR-Loop-Example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 

Repository files navigation

Deferred-Angular-FOR-Loop-Example

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.

Problem Description:

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.

Answer:

You can iterate manually and advance the iteration only when the previous promise finishes.

Why not just use FOR?

When using a FOR, the loop continues to execute without waiting for whatever deferred functions need to execute inside the loop.

Why not just use Angular.forEach?

Same problem as above.

Why not just use Angular's all() defer?

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.

Why this Deferred FOR Loop?

If you want to execute promises SEQUENTIALLY, this example is exactly what you need

About

An example of doing a deferred FOR loop in AngularJS, which is handy if you have to finish function execution inside the for loop sequentially, before going to the next iteration of a loop.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published