JavaScript AggregateError and the Array Family

Syed Muhammad Ali Raza - Sep 13 '23 - - Dev Community

Introduction

One of the most popular programming languages, JavaScript is constantly evolving to meet the demands of modern web development. With each iteration, new features and capabilities are introduced to make developers' and coders' lives more efficient.

In this article, we will look at two interesting aspects of JavaScript: AggregateError and the Array family, including Array, ArrayBuffer, AsyncFunction, AsyncGenerator, and AsyncGeneratorFunction. These features provide powerful tools for error handling and data management, making JavaScript a versatile and reliable language for many applications.

JavaScript's AggregateError

Understand the need

Some errors in JavaScript can be difficult to handle politely, especially when dealing with asynchronous operations. In the past, developers had to use workarounds such as creating error collections or custom error objects to collect multiple error instances. However, JavaScript introduced AggregateError in ECMAScript 2021 (ES12) to simplify the process.

What is AggregateError?

AggregateError is a built-in error class in JavaScript specifically designed to aggregate multiple errors into a single error problem. It inherits from the default "error" object, which makes handling easier. When multiple errors occur in Promise.allSettled() or Promise.any() operations, they are wrapped in an AggregateError object, preserving their identities.


try {
  // a set of promises that can be resolved or denied
  const promise = [Wise.resolve(1), Promise.reject("error 1"), Promise.reject("error 2")];
  const result = await Promise.allSettled(promise);

  // Check for total errors
  if (results.some((result) => result.status === 'rejected')) {
    throw new AggregateError(results.map((result) => result. reason));
  }
} catch (error) {
  if (AggregateError error) {
    console.error("An error occurred:", error.errors);
  } else {
    console.error("error:", error);
  }
}

Enter fullscreen mode Exit fullscreen mode

Uses AggregateError

  1. Clarity and Transparency: AggregateError provides a clear and structured way to handle multiple errors, making debugging and error messages more straightforward.

  2. Separate Error Storage: Stores separate error information, allowing developers to access details about each error that occurs.

  3. Consistency: Complies with existing error handling practices in JavaScript by conforming to the inheritance hierarchy of the "error" object.

Array Family

JavaScript's Array family consists of several important objects, each of which has a unique purpose in manipulating and storing data. Let's explore each one briefly:

1. Array

An Array object is the most basic member of the Array family. Used to store and manage a collection of ordered values. Arrays are versatile and support a variety of methods for adding, removing, and iterating over elements.


const fruit = ["apple", "banana", "cherry"];
fruits.push("date");
console.log(fruit); // ["apple", "banana", "cherry", "date"]

Enter fullscreen mode Exit fullscreen mode

2. ArrayBuffer

ArrayBuffer is a low-level object used to efficiently handle binary data. Represents a fixed-length buffer of binary data and is often used in conjunction with arrays or data types written to handle binary data.


const buffer = new ArrayBuffer(16);
const type = new Int32Array(buffer);
view[0] = 42;
console.log(view[0]); // 42

Enter fullscreen mode Exit fullscreen mode

3. AsyncFunction

The AsyncFunction object is used to define asynchronous functions. Using the "async" and "await" keywords allows you to declare promise functions that make it easier to work with asynchronous code.


const fetchData = async() => {
  const response = await receive("https://example.com/api/data");
  return response.json();
};

Enter fullscreen mode Exit fullscreen mode

4. AsyncGenerator

The AsyncGenerator object is used to define an asynchronous generator function. This function produces an asynchronous iterator that can be used to iterate over asynchronous series of data, making it easier to work with streams and asynchronous data sources.


async function * asyncDataGenerator() {
  await yield("https://example.com/data/1");
  await yield("https://example.com/data/2");
}

Enter fullscreen mode Exit fullscreen mode

5. AsyncGeneratorFunction

AsyncGeneratorFunction is a constructor for an Asynchronous generator function. Used to create instances of asynchronous generator functions, "Function" creates sequential functions.


const asyncGenFunc = async function *() {
  await yield("https://example.com/data/1");
};
const asyncGenerator = asyncGenFunc();

Enter fullscreen mode Exit fullscreen mode
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player