Company logo
  • Empleos
  • Bootcamp
  • Acerca de nosotros
  • Para profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
    • Bootcamp
  • Para empresas
    • Inicio
    • Nuestro proceso
    • Planes
    • Pruebas
    • Nómina
    • Blog
    • Calculadora

0

66
Vistas
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); });

7 months ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

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);
7 months ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar empleo Planes Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2023 PeakU Inc. All Rights Reserved.