Two JavaScript experimental features that I'm already dreaming of implementing on my codebase.

Two JavaScript experimental features that I'm already dreaming of implementing on my codebase.

AggregateError()

AggregateError object takes multiple iterable Error instances and wraps them in a single error.

Syntax?

AggregateError(Array<Error>, "message");
AggregateError Syntax

How to throw an AggregateError?

throw new AggregateError([
    new Error("Required field missing");
    new TypeError("Looking for string by array found");
], "All errors in your operation");
Throw an AggregateError

How to catch an AggregateError?

try {
    throw new AggregateError([
        new Error("Required field missing");
        new TypeError("Looking for string by array found");
    ], "All errors in your operation");
} catch(e) {
    console.log(e instanceof AggregateError); //true
    console.log(e.message) //All errors in your operation
    console.log(e.name) //AggregateError
    console.log(e.errors); // [Error: "Required field missing", Error: "Looking for string by array found"]
}
Catching an AggregateError

Use cases

  1. During Promise.all or Promise.any all errors from all async operations can be caught with a single error object. Helps you provide detailed error messages.
  2. If you are an API developer, you can collect all validation errors in an array for a call and report it once, instead of going for a round trip. Most of you may already do it with different ways, AggregateError makes it easy by instantiating one object for each request and lets you collect many errors from ACLs, Authentication, Validation etc.
  3. You don't have to interrupt an execution in the middle by throwing an Error object. You can let it pass and collect all errors in an array and finally deal with the errors if at all required.

MDN Docs

AggregateError
The AggregateError object represents an error when several errors need to be wrapped in a single error.

Pipeline Operator

Functions with a single parameter, the value of a parameter can be piped into a function.

Syntax

expression |> function
Syntax for Pipeline Operator

Example

-5 |> Math.abs; // Output: 5
Pipeline Operator Example

Chaining

Let's take an underscore.js library's example,

var stooges = [{name: 'curly', age: 25}, {name: 'moe', age: 21}, {name: 'larry', age: 23}];

stooges |> reverse |> last; //Output: {name: 'curly', age: 25}
Chaining with Pipeline Operator

With this new way of chaining methods in JavaScript, it gets rid of the whole debate about weather method chaining is a good practice or not.

Unknowns

  1. As it takes only one parameter, do they allow more complex syntax? Like,
var names = ["John", "Sam", "Dave", "Bob"];
var age = [30, 16, 27, 35];
var getAgeFor = "Sam";

var ageOfSam = age[getAgeFor |> names.indexOf];


Pipeline operator inside array index syntax

2. Can they play a role in async/await functions? Imagine how clean the code will look.

3. Can they also play a role for generator functions?

MDN Docs

Pipeline operator
The experimental pipeline operator | (currently at stage 1) pipes the value of an expression into a function. This allows the creation of chained function calls in a readable manner. The result is syntactic sugar in which a function call with a single argument can be written like this:

Summary

  1. AggregateError helps you collect all Error objects as one object.
  2. Pipeline Operator makes calling functions with single parameter clean and helps you chain methods.
Show Comments