Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

150
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda