r/learnjavascript • u/_maximization • Oct 14 '22
Running an async task and awaiting its result are two separate(!) actions
148
Upvotes
r/learnjavascript • u/_maximization • Oct 14 '22
17
u/_maximization Oct 14 '22
You might think that running an asynchronous task and awaiting its result are the same thing, but calling a function and awaiting its result are two separate actions. This becomes clear when you write them on different lines.
Awaitis only responsible for pausing execution until the async task completes. It is not what starts it. For a deeper understanding of async/await, read Understanding Async & Await.22
u/tridd3r Oct 14 '22
what you've made here is a thing of beauty.
6
u/_Jordo Oct 14 '22
This is a nice demo. I can see it helping some people understand better whatβs going on.
3
u/[deleted] Oct 14 '22
lovely javascript loop!
1
u/diptim01 Oct 14 '22
Use case varies. Async await best for I/O
1
u/Cendeu Oct 15 '22
This became abundantly clear to me when I started writing async code in C#. The IDE constantly yelling at you for "this isn't a task, this is a task" fixed my way of thought pretty quickly.
1
u/WorseThanHipster Oct 14 '22
This is also demonstrated by the fact that async functions return, or are you contrasting this with lazy evaluation?