Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

151
Vistas
How to update list in catch block

I have a small custom polyfill for Promise.all and while trying to execute it I see that update of array(errorList) in catch block is not working. Can someone please help me understand what am I missing.

Promise.customAll = function (promisesList) {
  let errorList = [];
  let resultList = [];
  return new Promise((resolve, reject) => {
    promisesList.forEach(async (promise, index) => {
      try {
        let result = await promise;
        resultList[index] = (result);
      } catch (err) {
        errorList.push(err);
      }
    });

    if (errorList.length) {
      reject(errorList);
    } else {
      resolve(resultList);
    }
  });
};

Here is how I am using it. For above code I am expecting my promise tp go to catch block but its going to then block and printing [ 'FirstPromise', <1 empty item>, '3rd Promise' ]

Promise.customAll([Promise.resolve("FirstPromise"), Promise.reject("Error in 2nd Promise"), Promise.resolve("3rd Promise")])
  .then((val) => {
    console.log(val);
  })
  .catch((err) => {
    console.log(err);
  });
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Promise.customAll = function (promisesList) {
  let errorList = [];
  let resultList = [];
  return new Promise((resolve, reject) => {
    promisesList.forEach(async (promise, index) => {
      try {
        let result = await promise;
        resultList[index] = result;

        if (index === promisesList.length - 1) {
          if (errorList.length) {
            reject(errorList);
          } else {
            resolve(resultList);
          }
        }
      } catch (err) {
        errorList.push(err);
      }
    });
  });
};

As suggested by @youdateme, moved my logic in forEach callback as its async that solved the issue.

about 4 years 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 vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda