123123123123
123123123123

Reputation: 3

why is the error not being processed in async function?

const promiseAllAsyncAwait = async function() {
  if (!arguments.length) {
    return null;
  }
  let args = arguments;
  if (args.length === 1 && Array.isArray(args[0])) {
    args = args[0];
  }
  const total = args.length;
  const result = [];
  for (let i = 0; i < total; i++) {
    try {
      const res = await Promise.resolve(args[i]);
      if (res) {
        result.push(res);
      }
    } catch (err) {
      console.log(123);
      return err;

    }
  }
  return result;
};

const asyncTask1 = new Promise((resolve) => setTimeout(() => resolve('Task 1'), 1000));
const asyncTask2 = new Promise((resolve, reject) => setTimeout(() => reject('Error in Task 2'), 500));
const asyncTask3 = new Promise((resolve) => setTimeout(() => resolve('Task 3'), 1500));

(async () => {
  try {
    const results = await promiseAllAsyncAwait([asyncTask1, asyncTask2, asyncTask3]);
    console.log(results);
  } catch (error) {
    console.error('Error in promiseAllAsync:', error);
  }
})();

it should output 123 to the console, but it does not output. it just outputs this error '[UnhandledPromiseRejection: This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason "Error in Task 2".] { code: 'ERR_UNHANDLED_REJECTION' }'

Upvotes: 0

Views: 76

Answers (3)

Bergi
Bergi

Reputation: 665286

It looks like you are trying to re-implement Promise.all using only async/await. That is not possible, since with await you can only wait for one promise after the other. However, Promise.all needs to register a handler on all passed promises immediately, to be able to handle rejections as soon as possible (by forwarding the error and rejecting the returned promise).

Your code would eventually output 123, but the unhandled promise rejection (asyncTask2 is rejected before your code would await it) crashes your nodejs process.

Upvotes: 0

Mudassir Qureshi
Mudassir Qureshi

Reputation: 1

The issue in your code is that when a promise is rejected inside the try block of promiseAllAsyncAwait, you are catching the error and logging "123," but then you are returning the error immediately. This causes the overall promise returned by promiseAllAsyncAwait to be rejected with the first encountered error.

To address this, you can modify your code to continue processing the remaining promises even if one is rejected. You can store the errors encountered in a separate array and then return both the successful results and the errors at the end. Here's an updated version of your code:

const promiseAllAsyncAwait = async function() {
  if (!arguments.length) {
    return null;
  }
  let args = arguments;
  if (args.length === 1 && Array.isArray(args[0])) {
    args = args[0];
  }
  const total = args.length;
  const result = [];
  const errors = [];
  
  for (let i = 0; i < total; i++) {
    try {
      const res = await Promise.resolve(args[i]);
      result.push(res);
    } catch (err) {
      console.log(123);
      errors.push(err);
    }
  }
  
  if (errors.length > 0) {
    return { result, errors };
  } else {
    return result;
  }
};

const asyncTask1 = new Promise((resolve) => setTimeout(() => resolve('Task 1'), 1000));
const asyncTask2 = new Promise((resolve, reject) => setTimeout(() => reject('Error in Task 2'), 500));
const asyncTask3 = new Promise((resolve) => setTimeout(() => resolve('Task 3'), 1500));

(async () => {
  try {
    const results = await promiseAllAsyncAwait([asyncTask1, asyncTask2, asyncTask3]);
    console.log(results);
  } catch (error) {
    console.error('Error in promiseAllAsync:', error);
  }
})();

This way, even if one promise is rejected, the code will continue processing the remaining promises, and you'll get both successful results and errors at the end. In your example, it should log "123" to the console.

Upvotes: -1

ohfufu
ohfufu

Reputation: 321

you should throw the error in the catch block inside the promiseAllAsyncAwait function instead of returning it.

const promiseAllAsyncAwait = async function() {
  if (!arguments.length) {
    return null;
  }
  let args = arguments;
  if (args.length === 1 && Array.isArray(args[0])) {
    args = args[0];
  }
  const total = args.length;
  const result = [];
  for (let i = 0; i < total; i++) {
    try {
      const res = await Promise.resolve(args[i]);
      if (res) {
        result.push(res);
      }
    } catch (err) {
      console.log(123);
      throw err; // Throw the error instead of returning it
    }
  }
  return result;
};

const asyncTask1 = new Promise((resolve) => setTimeout(() => resolve('Task 1'), 1000));
const asyncTask2 = new Promise((resolve, reject) => setTimeout(() => reject('Error in Task 2'), 500));
const asyncTask3 = new Promise((resolve) => setTimeout(() => resolve('Task 3'), 1500));

(async () => {
  try {
    const results = await promiseAllAsyncAwait([asyncTask1, asyncTask2, asyncTask3]);
    console.log(results);
  } catch (error) {
    console.error('Error in promiseAllAsync:', error);
  }
})();

Upvotes: -1

Related Questions