JavaScript and Asynchronous Operations
Do you really do it right?
Asynchronous operations are not a mystery but there is a catch: do you really take the benefits from it?
Know how to properly control the async flow is an important benchmark in a developer career and it can easily be the difference between a “yes” or “no” during a technical interview or even in the thing that can boost the performance of your app.
So let’s get some concepts right:
1 — All async functions returns a Promise
, so you have available the methods .then()
, .catch()
and .finally()
.
2 — Async functions when called, are executed in the background. Once resolved (finished), it will call the callback function to let you know about the result (fulfilled or rejected).
3 — The “await” keyword blocks the code execution on that line until the Promise is settled (fulfilled or rejected); only then, the next line will run.
So, what are the actual effects we want to have when using async ops?
That’s simple: performance.
And it’s done by running all Promises in the background in a non-blocking way; optimally using Promise.allSettled()
method.