Did you know that we can optimize this function to complete in as little as half of the time?
In this function, we’re awaiting a fetch for a user, and then a fetch for a product, sequentially.
But one doesn’t depend on the other, so we don’t have to wait for one to complete before we fire off the request for the next.
Instead, we could fire both requests together, and await both concurrently.
One way to do this is utilizing
Promise.all, like so:
And now if we imagine that each of those requests took 1 second to respond each, whereas in our original function we would wait for both in a row totaling 2 seconds for our function to complete, in this new function we wait for both concurrently so our function completes in 1 second — half the time!
But… theres just one problem
First, we’re not handling errors at all here.
So you could say “sure, I’ll put this in a big ole try-catch block”.
But this actually has a major issue.
fetchUser completes first with an error. That will trigger our catch block and then continue on with the function.
But here is the kicker - if
fetchProducts then errors afterward, this will not trigger the catch block. That is because our function has already continued. The catch code has run, the function has completed — we’ve moved on.
So this will instead result in an unhandled promise rejection. Ack.
So if we have some kind of handling logic, that prompts the user or saves to an error logging service, like so:
We will sadly only be made aware of the first error. The second error will be lost in the ether - with no user feedback, not being captured in our error logs - it’s effectively invisible (besides a little noise in the browser console).
One solution to our above issue is to pass a function to
.catch(), for instance like this:
In this case, if we get an error, we return handle the error and return it. So now our resulting
product objects are either an
Error, which we can check with
instanceof, or otherwise our actual good result.
This ain't so bad, and solves our prior issues.
But, the main drawback here is we need to make sure we are always providing that
.catch(onReject) , religiously, throughout our code. This is sadly quite easy to miss, and also not the easiest to write a bullet proof eslint rule for.
As a side note, it’s useful to keep in mind that we don’t always need to immediately await a promise after creating it. Another technique that we can use here that is virtually the same is this this:
Because we fire off each fetch before we await for either one, this version has the same performance benefits as our examples above that use
Additionally, in this format, we can safely use
try/catch if we like without the issues we had previously:
Between these three, I personally like the
Promise.all version, as it feels more idiomatic to say “wait for these two things together”. But that said, I think this just comes down to personal preference
Promise.allSettled, instead of getting the
product back directly, we get a result object that contains the value or error of each promise result.
The result objects have 3 properties:
value- Only present if
"fulfilled". The value that the promise was fulfilled with
reason- Only present if
"rejected". The reason that the promise was rejected with.
So we can now read what the status of each promise was, and process each error individually, without losing any of this critical information:
But, that is kind of a lot of boilerplate. So let’s abstract this down:
And we can implement a simple
handleResults function like so:
We are able to use a nifty trick here, the AggergateError class, to throw an error that may contain multiple inside. This way, when caught we get a single error with all details, via the
.errors property on an
AggregateError that includes every error included:
And hey, this is pretty simple, nice and generic. I like it.
Don’t miss the warnings on these though (below), as while they are interesting and occasionally useful, I would use them with caution.
One method we get is
Promise.race, which takes an iterable of promises and returns a single
Promise that settles with the eventual state of the first promise that settles.
For example, we could implement a simple timeout like so:
⚠️ Note: this isn’t always ideal, as generally if you have a timeout you should cancel the outstanding pending task if at all possible.
So for example if
doSomethingSlow() fetched data, we’d generally want to abort the fetch upon a timeout using an AbortController instead of just rejecting the race promise and moving about our business, letting the request continue to hang for no good reason.
But this is just an example to demonstrate the basic concept, so hopefully you get the idea.
Also, like always, it’s still best to handle all errors of all promises as well:
The final method we get is
Promise.any, which is similar to
Promise.race, but instead waits for either promise to resolve successfully, and only rejects if both promises reject.
This can be useful, for example, in situations where it can be unpredictable which location for a piece of data is faster:
Similar to the above, an ideal solution here would abort the slower request once the faster one completes. But again - these are just simple contrived examples to demonstrate the basics.
⚠️ Note: we don’t always want to hammer on multiple data sources concurrently just because we can (e.g. just because it might save the user a fraction of a second). So use this wisely, and sparingly.
Oh yeah, and like always, we really don’t want unhandled promise rejections, so you know what to do:
Before we get too excited and concurrentify all of our code, let’s not forget three things.
Concurrency is awesome, but excessive parallelization can lead to network thrashing, disk thrashing, or other issues. Use good judgment and avoid craziness like this:
Just to be sure we avoid confusion - I want to point out that it’s important to be aware that when we are talking about concurrency here, we are referring to awaiting promises concurrently, not executing code concurrently.
Sometimes sequential code is simply easier to reason about and manage.
Avoid premature optimization and be sure you have a good reason before adding more complexity. Being fast is great, but consider if it’s even needed before blindly concurrentifying everything in your code.
Use tools to solve problems, not create them.
We make a way to drag + drop with your components to create pages and other CMS content on your site or app, visually.
You may find it interesting or useful: