AggregateError()
AggregateError object takes multiple iterable Error
instances and wraps them in a single error.
Syntax?
How to throw an AggregateError?
How to catch an AggregateError?
Use cases
- During
Promise.all
orPromise.any
all errors from all async operations can be caught with a single error object. Helps you provide detailed error messages. - 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.
- 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
Pipeline Operator
Functions with a single parameter, the value of a parameter can be piped into a function.
Syntax
Example
Chaining
Let's take an underscore.js library's example,
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
- As it takes only one parameter, do they allow more complex syntax? Like,
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
Summary
- AggregateError helps you collect all Error objects as one object.
- Pipeline Operator makes calling functions with single parameter clean and helps you chain methods.