[UnhandledPromiseRejection]: Why try…catch handle promise rejection if the last promise rejects, but throws an error for intermediate rejections?

async function all(...params) {
  let result = [];
  for (let promise of params) {
    try {
      let res = await promise;
      result.push(res);
    } catch (e) {
      result.push({
        error: e
      });
    }
  }
  return result;
}

const promise1 = new Promise((resolve, reject) => {
  setTimeout(() => {
    resolve(3);
  }, 3000);
});

const promise2 = new Promise((resolve, reject) => {
  setTimeout(() => {
    reject(1);
  }, 1000);
});

const promise3 = new Promise((resolve, reject) => {
  setTimeout(() => {
    resolve(2);
  }, 2000);
});

all(promise1, promise2, promise3).then(console.log);

I’m working with an async function in JavaScript that processes multiple promises using a try…catch block to handle any potential errors.

I have tried debugging and found that the try catch works for if the last promise rejects and throws error if any intermediate promise rejects.

  1. Why does the try…catch seem to only work if the last promise rejects?
  2. How can I modify my code to correctly handle rejections for all promises without triggering unhandled promise rejections?