0

Similar question was already asked here, but I am interested, how to get the result, when all promises finished.

In my code I am installing files to a device - this needs to be done sequentially and install creates a promise.

Inspired by the above example and by the example from BluebirdJS documentation I came with following code:

  Promise.each(files, function(file, index){
    self.adbClient.install(device, file.path)
    .then(function() {
      console.log('%s installed on device %s', file.name, device)
    })
    .catch(function(err) {
      console.error('Something went wrong when installing %s on %s: '+  err.stack, file.name, device)
    })
  }).then(function() { console.log("All done"); })
  .catch(function(err) { console.log("Argh, broken: " + err.message); })

After execution I get:

All done
first-file.apk installed on device LGV400d1d1a81d
second-file.apk installed on device LGV400d1d1a81d

How can I get the All done message after all installations are completed ?

Community
  • 1
  • 1
karlitos
  • 1,604
  • 3
  • 27
  • 59

1 Answers1

0

According to the correct documentation http://bluebirdjs.com/docs/api/promise.each.html -

If the iterator function returns a promise or a thenable, then the result of the promise is awaited before continuing with next iteration

So, it's a simple case of adding a return as follows

 Promise.each(files, function(file, index){
    return self.adbClient.install(device, file.path)
 // ^^^^^^
 // rest of your code
Jaromanda X
  • 53,868
  • 5
  • 73
  • 87