CC-Archived / promise-as3

Promises/A+ compliant implementation in ActionScript 3.0
167 stars 52 forks source link

Zip implementation #21

Closed ryandbair closed 10 years ago

ryandbair commented 11 years ago

I came across a case where I need to combine multiple Promises into one, similar to other Promise implementation's zip feature (such as Scala's).

I put together a solution which can be used like so:

promise1.zip(promise2,promise3).then(
function(a:Array):int { /* do stuff with results */ }
);

I didn't see anything like this in the Promises/A+ specs, but I think it would be useful to many people. If its something you would like to include, I can clean up the code and submit a pull request.

Interested?

johnyanarella commented 10 years ago

I just added a Promise.all() utility function which addresses this need similarly to how the various JavaScript Promises/A+ implementations have.

Promise
  .all( [ promise1, promise2, promise3 ] )
  .then( function ( fulfilledValues:Array ):int {
    // do stuff with results
  } );

NOTE: The parameter passed to Promise.all() can be an Array of values and/or Promises, or even a Promise of an Array of values and/or Promises.

You might also be interested in the new spread utility function, too:

import com.codecatalyst.util.spread;

// ...

Promise
  .all( [ promise1, promise2, promise3 ] )
  .then( spread( function( value1:int, value2:int, value3:int ):int {
    // do stuff with results
  } ) );