The exercise is from this section of that Chapter:
Solution code:
if (!Promise.first) {
Promise.first = function(prs) {
var errArr = []; // store the reject reasons
return new Promise( function(resolve,reject){
prs.forEach( function(pr){
Promise.resolve( pr )
.then(
resolve,
function(err) {
errArr.push(err);
if(errArr.length == prs.length) {
reject(errArr); // if all promises rejected, reject the returned promise
}
});
});
});
};
}
Below is how its used:
// say foo(), foo1(), foo2() are three promises that are rejected
Promise.first([foo(), foo1(), foo2()])
.catch(function(err){
console.log(err);
});