I want to use the when.map function to process some data.
After the data is processed I need to do some cleanup (e.g. release the currently used database connection back to the connection pool).
The problem with my approach using catch and finally  is that finally is called when the first reject occurs, and while other mappings are still in progress.
So how can I wait until all of the mapping promises are finished, so that it is possible to do a save cleanup.
  require('when/monitor/console');
  var when = require('when');
  function testMapper(value) {
    console.log('testMapper called with: '+value);
    return when.promise(function(resolve, reject) {
      setTimeout(function() {
        console.log('reject: '+value);
        reject(new Error('error: '+value));
      },100*value);
    });
  }
  when.map([1,2,3,4],testMapper)
  .then(function() {
    console.log('finished')
  })
  .catch(function(e) {
    console.log(e);
  })
  .finally(function() {
    console.log('finally')
  });
Output
 testMapper called with: 1
 testMapper called with: 2
 testMapper called with: 3
 testMapper called with: 4
 reject: 1
 [Error: error: 1]
 finally
 reject: 2
 [promises] Unhandled rejections: 1
 Error: error: 2
     at null._onTimeout (index.js:9:14)
 reject: 3
 [promises] Unhandled rejections: 2
 Error: error: 2
     at null._onTimeout (index.js:9:14)
 Error: error: 3
     at null._onTimeout (index.js:9:14)
 reject: 4
 [promises] Unhandled rejections: 3
 Error: error: 2
     at null._onTimeout (index.js:9:14)
 Error: error: 3
     at null._onTimeout (index.js:9:14)
 Error: error: 4
     at null._onTimeout (index.js:9:14)  
Environmentinformation:
- whenjs: v3.1.0
- node: v0.10.26
 
    