If you started your developer career about a decade ago, you probably know how complicated it was to use AJAX in our web applications. However, by ...
For further actions, you may consider blocking this person and/or reporting abuse
Adding a little, this piece of code:
Can be written like this:
Or even like this:
And from my point of view, that's way more readable than the
async/awaitapproach, tho I know that people not familiar with async code coming from other languages findasync/awaiteasier to read.Cheers!
I don't think it is easier to read. Besides if you have this:
The order of execution requires you to know that there is a promise here so that the functions are called in this order:
1- fetch
2- bar
3- foo
Hiding async code with syntax sugar is not the solution to that problem... and your over simplistic example is not taking into consideration that with
async/awaityou have to wrap stuff in atry/catch, so let's say you "solve" yourfoo/barproblem withasync/await:The order is great! ... or is it? Was the intention to run the
barfunction either way? Or maybe just whenfetchdoesn't fail?And with
.thenyou have the same "issue":From my point of view, this last snippet is still way more clear and less convoluted than the
try/catch/async/awaitone. Maybe you still prefer that one, but as I mentioned we need to compare them in less simplified scenarios, and in that case both are kinda equally complex, but thethen/catchapproach requires less code while still being readable.Cheers!
Well, you are not wrong because this is pretty much a personal preference.
I just do not agree with you. 😎
Lastly, all the possible options you mention although correct, I think they miss the point.
If you have this:
The point is to refactor into async/await but keep the exact same behaviour.
It does not matter much what that behaviour is on the example (functions don't even have a body here).
Cheers! 🍻 happy coding
My point was that mainly, in real world scenarios, you have to handle errors, so you'll have to write that
try/catchat some point. I also shown several scenarios that might be tricky with either approach. Not to mention that is pretty common to forget that as soon as you make a functionasync, you make it return aPromiseas well, even if you're not actually using promises inside, or if you wanted to handle it internally, so is more of a "footgun" thanthen/catch. Going back to the simplistic example:Maybe your intention IS to run both
fetchandbar"at the same time", which is quite easy to achieve that way, but if you usedawait, then runningbarat the same time is a little bit more complicated. Not to mention, you can combineasync/awaitwiththen/catchas well, so is not that we need to chose one OR the other:But yeah, as you mentioned, is generally a preference thing. I tend to use
async/awaitonly when thethenalternative actually is less readable (like withAsyncIterables).Hey @lukeshiru and @vetras, thanks for sharing your points. I think both ways are completely fine and I also think it depends on personal/team preferences.
Super helpful
Thank you!
Can you get progress information and partial results with the fetch API?
Hey Jakub, I think I didn't get your questions. Can you explain it a little more?
With AJAX in
onreadystatechangeyou can not only check whether you got full response, or an error, but also if you got partial response. It is useful for generating progress information if the response is slow (you receive results in spurts) - but of known final size, or for generating partial results.Hi Jakub, for progress and partial results Server Sent Events (SSE - developer.mozilla.org/es/docs/Web/...) may be what you’re looking for.
helpful
Thank you!
Typo in the last example?
Hey Mehuge, what do you mean by Typo?
Thank you!
You're welcome!! :)
Thank you
You're welcome!! :)
Really good article with lots of info.
Thank you!
So helpful. Thx
Thank you!
Very well organized, easy to understand thanks, am following you 😌
I appreciate it. Thank you! :)
Thanks for putting it all together in one concept pointing out all the different possibilities. Very clear and to the point.
Thank you for your comment. I'm really glad you liked it.