Using promises in a loop


#1

I have the following “model” method at my route. I am going thought the collection of transfer objects and do some filtering which is actually removed for simplicity. However, if there is a flag for recurring transfer, I am doing call to App.TransferModel.fetchRecurringTransfer method that has a promise inside.

Only after transfers collection will be fully updated, I need to go another method that will do some work with this collection and it will be presented to template. It is like I need a wait till each call for recurring data will be completed before doing another call. It is not clear how many transfers are actually recurring. I just need a collection, fully loaded with recurring data, before I would do other processing. How I would do that with Ember? Thanks!


#2

I think what you want is Ember.RSVP.allSettled.

So you can just do return Ember.RSV.allSettled(transfers);. You should be aware that allSettled will not assert on promise failures. The success handler will provide you with a map of the fulfilled and rejected promises. Docs explain more. :slight_smile:


#3

Nope - return Ember.RSVP.allSettled(transfers) does not make any difference!


#4

That’s because you need to collect the promises you create in your loop, gather them in an array and pass that array to allSettled.


#5

yup - this is what I found out. Thanks!


#6

Yeap. Good catch. I didn’t look closely enough at the loops :smile: