My `Promise.first` solution for the exercise of the Chapter Promise, Async and Performance, YDNJS

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 )
          function(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()])