2

I'm looping through an array, when all fileRead deferred are resolved, 'done' is logged.

$.when.apply(null, $.each(files, function(index, file){

        return self.fileRead.read(file).done(function(fileB64){
            self.fileShow(file, fileB64, fileTemplate);
        });

    })).done(function() {  
        console.log('done');

    })

The problem is, I only wish done to be logged once the fileShow method has returned.

  1. Does this fileShow method also need to implement deferred. Or can it just return?

  2. How can I modify the loop so console.log('done') is ran once all fileShow methods have been executed?

Rahul Gupta
  • 9,775
  • 7
  • 56
  • 69
user3729576
  • 237
  • 5
  • 10
  • 1
    Use `$.map` instead of `$.each` so you can return the `Deferred` objects. `$.each` returns its first argument. – Barmar Jun 19 '14 at 13:07

1 Answers1

0

Use $.map instead of $.each. This returns an array of the Deferred objects returned by your iteration function, and these can then be passed to $.when.

$.when.apply(null, $.map(function(index, file) {
    return self.fileRead.read(file).done(function(fileB64) {
        self.fileShow(file, fileB64, fileTemplate);
    });

})).done(function() {  
    console.log('done');

});
Barmar
  • 741,623
  • 53
  • 500
  • 612