-
Notifications
You must be signed in to change notification settings - Fork 263
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #113 from decasia/all-settled
Add allSettled to RSVP
- Loading branch information
Showing
3 changed files
with
199 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import Promise from "./promise"; | ||
import { isArray, isNonThenable } from "./utils"; | ||
|
||
/** | ||
`RSVP.allSettled` is similar to `RSVP.all`, but instead of implementing | ||
a fail-fast method, it waits until all the promises have returned and | ||
shows you all the results. This is useful if you want to handle multiple | ||
promises' failure states together as a set. | ||
Returns a promise that is fulfilled when all the given promises have been | ||
settled. The return promise is fulfilled with an array of the states of | ||
the promises passed into the `promises` array argument. | ||
Each state object will either indicate fulfillment or rejection, and | ||
provide the corresponding value or reason. The states will take one of | ||
the following formats: | ||
```javascript | ||
{ state: "fulfilled", value: value } | ||
or | ||
{ state: "rejected", reason: reason } | ||
``` | ||
Example: | ||
```javascript | ||
var promise1 = RSVP.resolve(1); | ||
var promise2 = RSVP.reject(new Error("2")); | ||
var promise3 = RSVP.reject(new Error("3")); | ||
var promises = [ promise1, promise2, promise3 ]; | ||
RSVP.allSettled(promises).then(function(array){ | ||
// array == [ | ||
// { state: "fulfilled", value: 1 }, | ||
// { state: "rejected", reason: Error }, | ||
// { state: "rejected", reason: Error } | ||
// ] | ||
// Note that for the second item, reason.message will be "2", and for the | ||
// third item, reason.message will be "3". | ||
}, function(error) { | ||
// Not run. (This block would only be called if allSettled had failed, | ||
// for instance if passed an incorrect argument type.) | ||
}); | ||
``` | ||
@method @allSettled | ||
@for RSVP | ||
@param {Array} promises; | ||
@param {String} label - optional string that describes the promise. | ||
Useful for tooling. | ||
@return {Promise} promise that is fulfilled with an array of the settled | ||
states of the constituent promises. | ||
*/ | ||
|
||
export default function allSettled(entries, label) { | ||
return new Promise(function(resolve, reject) { | ||
if (!isArray(entries)) { | ||
throw new TypeError('You must pass an array to allSettled.'); | ||
} | ||
|
||
var remaining = entries.length; | ||
var results = new Array(remaining); | ||
var entry; | ||
|
||
if (remaining === 0) { | ||
resolve([]); | ||
} | ||
|
||
function fulfilledState(value) { | ||
return { state: "fulfilled", value: value }; | ||
} | ||
|
||
function rejectedState(reason) { | ||
return { state: "rejected", reason: reason }; | ||
} | ||
|
||
function fulfilledResolver(index) { | ||
return function(value) { | ||
resolveAll(index, fulfilledState(value) ); | ||
}; | ||
} | ||
|
||
function rejectedResolver(index) { | ||
return function(reason) { | ||
resolveAll(index, rejectedState(reason) ); | ||
}; | ||
} | ||
|
||
function resolveAll(index, value) { | ||
results[index] = value; | ||
if (--remaining === 0) { | ||
resolve(results); | ||
} | ||
} | ||
|
||
for (var index = 0; index < entries.length; index++) { | ||
entry = entries[index]; | ||
|
||
if (isNonThenable(entry)) { | ||
resolveAll(index, fulfilledState(entry) ); | ||
} else { | ||
Promise.cast(entry).then( fulfilledResolver(index), rejectedResolver(index) ); | ||
} | ||
} | ||
}, label); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters