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'
    ) {
    } else {

  Promise.resolve('test_value')]).then(value => { console.log(value); });


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


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);