skip to Main Content
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’
}’

2

Answers


  1. 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);
      }
    })();
    
    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search