How it works...

As we have seen before, Promise.all returns a Promise that resolves to an array of values resolved from multiple values, and we can await those values when they are fulfilled. When we await a Promise that is resolved in an error state, an exception is thrown.

What is interesting about the preceding code is that three asynchronous promises are executing concurrently. If one or more of them is fulfilled with an error, then one or more errors will be thrown.

You'll notice that only one error is ever caught and logged. As with synchronous code, we may potentially have multiple errors thrown by our code, but only one will be caught and logged by the catch block.

Get ECMAScript Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.