Is Promise.all enough?

Hey friend,

Today we will talk about Promises šŸ¤. But first, let's understand what a promise is. A promise has 3 states: pending, fulfilled, and rejected. Here is the breakdown

  • Pending: the promise is still in the works.
  • Fulfilled: the promise resolves successfully and returns a value.
  • Rejected: the promise fails with an error.

When a promise is returned, it could be any of the above values.

Let's review our weekly snack šŸ‘‡

What if there are several promises that we want to resolve? Well, then we might have heard of `Promise.all`.

What is Promise.all?

Promise.all will allow us to invoke all promises in the form of an array and will return an array of resolved values.

But wait, hold on. There is a catch here. If one of the promises is rejected, then all of a sudden, the entire Promise chain is rejected. This is not usually what we want, as we still want our app to function as expected and don't want it to stop working if one thing doesn't work.

So how can we solve this problem? šŸ¤”

Introducing Promise.allSettled āœØ

Promise.allSettled will also return the resolved as well as rejected values of the promises unlike Promise.all. In case, one promise gets rejected, we will still get the resolved values of other promises. This way, our app will continue to function.

Latest Frontend Gossip/News šŸ¤«

ā€‹Introducing Svelte 1.0: A new version of Svelte is out. It's a framework used to build apps with server-side rendering but does things differently. Svelte is built on top of Vite.

ā€‹SWR 2.0 by Vercel: SWR 2.0 is out. SWR is a popular React data-fetching library that enables components to fetch, cache, and mutate data. SWR 2.0 makes it better by providing ease of use, additional hooks, and more optimizations to make it easier to fetch data.

ā€‹Vite 4.0 is out: Vite 4.0 is finally out and has exciting changes. We already saw Nuxt and Astro built on top of Vite.

Frontend Developers Toolbox šŸ§°

  • ā€‹Console Ninja: Console Ninja VS Code extension allows you to see console.log output and runtime errors right next to your code.
  • ā€‹Master Async: We know that async concepts can be hard. Here is a good walkthrough of mastering async. Over the course of 47 live examples and exercises, you'll make callbacks, promises and async/await feel like second nature.

Have a wonderful day!

A Frontend Newsletter that you'll love! šŸ’–