Stop Making This Async/Await JavaScript Mistake!

preview_player
Показать описание
This is one of the biggest differences I see between "pro" and beginner javascript code.

Prepping for your frontend interviews? Use code "conner" for a discount on my product FrontendExpert:

Рекомендации по теме
Комментарии
Автор

I cannot forget this approach, this is the same solution which landed me my current job.

imfadd
Автор

Also does not make sense to use async code when it’s a single task where each result is passed to the next function. The whole point of async code (most people forgot or don’t know) is to not block additional tasks from being accepted and started while waiting for network or disk IO

LouisRamos
Автор

Thanks for the tips on avoiding async/await mistakes! Super helpful and clear. Keep up the great work!

sucessdy
Автор

You could also just call all the promises and await them when you need to use them. Because promises are hot the computation starts as soon as you invoke them.

Cleaner to use the Promise statics but it’s useful to know you don’t need them.

kyuss
Автор

Great. Except in most cases, each promise depends on the data from the previous one, or depends on the validation results of previous promises... Eg. getting a user account out of a DB, validating the credentials and creating a session...
Doing them all in parallel would produce faster results, but would also cost the server MUCH more in terms of processing power for things that may ultimately result in errors.
I'd much rather have a alightly slower codebase than a much more costly one.

shapelessed
Автор

So as long as the results of one async function does not depend on the results of the others, you can run it separately from the others and at the same time?

RyanScarbrough
Автор

Does promise.all need an await? I vaguely remember it being able to return once the promises are all resolved... Or was that a dream I had

hikari
Автор

Awaiting each line is not wrong, it's just not optimal in certain situations where the calls are independent. But for calls that are dependent on the completion of the previous call, then you have no option but to run them consecutively.

parlor
Автор

Depending on the situation, Promise.all might result in overall-slower execution because you’re introducing an additional promise. If your application starts another asynchronous function (one not seen here) if that function takes a long time your Promise.all may still be waiting.

eqprog
Автор

Isn’t the issue with this approach that if one async call fails they all fail, since promise.all fails if any promise rejects?

gabrielkime
Автор

This is totally dependent on what your code needs to do. Does it need to wait for each other, or do they not rely on each other...

brandonm