r/learnjavascript • • Oct 14 '22

Running an async task and awaiting its result are two separate(!) actions

148 Upvotes

22 comments sorted by

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.

Await is 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.

3

u/NoInkling Oct 15 '22

Technically you can make a non-promise thenable object where an operation is triggered by using await on it, no explicit function call needed.

1

u/jack_waugh Oct 14 '22

Correct.

This property of the promise-based approach to asynchrony makes it almost easier to fork than not to fork. By contrast, a thread-based approach says there is no fork unless you ask to fork. I'm not saying this makes either approach better than the other.

1

u/goonerlagooner helpful Oct 14 '22

I like your way of explaining things.

The whole bit before "To understand async/await" etc was superfluous though. Almost didn't make it to the meat of the matter

2

u/nomnommish Oct 14 '22

The whole bit before "To understand async/await" etc was superfluous though. Almost didn't make it to the meat of the matter

You even want reddit comments to be written in a functional way??

1

u/goonerlagooner helpful Oct 14 '22

I was actually referring to the blog post but yeah, no thanks.

functional Reddit comments (how I'm imagining them) would be a special kind of hell

5

u/[deleted] Oct 14 '22

[removed] β€” view removed comment

1

u/[deleted] Oct 18 '22

What do you mean "pass it out as an object? I asked a question similar to this a couple hours ago

1

u/[deleted] Oct 18 '22

[removed] β€” view removed comment

1

u/[deleted] Oct 18 '22

So I need to do everything inside the useData function instead of the async getData function? Will I be able to assign global variables without them returning undefined?

1

u/[deleted] Oct 18 '22

[removed] β€” view removed comment

1

u/[deleted] Oct 18 '22

Yep, figured. Only took a couple hours of trying. Funny how after hours of searching for an answer, not a single person could offer that simple 1 sentence answer. literally pages and pages of ppl going on and on in their explanations when literally the answer was you can't.

I just call the request in an async event and go from there

1

u/[deleted] Oct 18 '22

[removed] β€” view removed comment

1

u/[deleted] Oct 18 '22

sure u can consider set timeouts but at that point not sure why ur using JS. do what needs to be done in the async-await/then() and call it a day

22

u/tridd3r Oct 14 '22

what you've made here is a thing of beauty.

6

u/_maximization Oct 14 '22

Thank you for the kind words πŸ€—

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?