Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

153
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!