Why am I getting an unexpected behavior in my implementation of Promise.race()?

I have the code for my Promise.race() implementation, and it basically works. However, in this case:

promiseRace([Promise.reject('test_error'), Promise.resolve('test_value')])

I am getting test_value instead of test_error.
Why it happens?

const promiseRace = promises => new Promise((resolve, reject) => {
  promises.forEach(p => {
    if (
      typeof p === 'object'
          && 'then' in p
          && typeof p.then === 'function'
    ) {
      p.then(resolve).catch(reject);
    } else {
      resolve(p);
    }
  });
});

promiseRace([Promise.reject('test_error'),
  Promise.resolve('test_value')]).then(value => { console.log(value); });

Answer

This happens because in the following line, you call .catch on another (chained) promise, not on the original promise:

  p.then(resolve).catch(reject);

Note how the then call does not get a reject callback argument, and so there is an extra — intermediate asynchronous promise cycle. By the time the .catch callback is called (reject), the other case has already been dealt with (the fulfulled promise).

So change to:

  p.then(resolve, reject);