Transcript from the "Async Function Exercise" Lesson
>> What we're gonna do now is we're gonna together fill in the code that we already know how to do. We already know the procedure for getting this data, and weighting it, and everything. We're gonna fill in TODO 3 to fill in the body of this async function.
[00:00:17] Okay, so we have this two to three, we have a fetchMessage (url). So, we've got our URL and we know how to fetch the resource at that URL into a response, all right? So, we're gonna work through this, you're gonna help me write out the thing that we've done like several times now, how do I capture that response?
[00:00:39] Well, what function do we use to get stuff from the internet?
>> Fetch. So I'm gonna fetch the URL, but I have to await the fetch. And then I wanna do something with that value, so I'm going to capture it as a variable. Now yeah, we've been doing a lot of let's around here and I've been not practicing what I preach of we don't need to reassign this so we can use a const here, great.
[00:01:02] Okay, then I need some body. [LAUGH] So how do I get the body of this response? One more time, we've done it few times now.
>> It's also await.
>> Await response.json, which is gonna parse that body. And then we want just the message property or the value of the message property.
[00:01:28] So, how do I do that with my destructuring?
>> Squigglies, yes. And I need my declaration keyword and we've been using let, but I could also use const. So in the squigglies, I'm gonna put the name of the property that I care about which is,
>> (message) =
>> body, and then I want to return the message, yeah. If this works, I should be able to paste it in here. So now I should be able to call fetchMessage on this URL, and is this gonna work? No, I have to add my friend await, and my gosh, we got out the dogs we care about.
[00:02:17] [APPLAUSE] Yeah, you see that one only took us a couple of minutes, nice. Okay, so now we've really flexed our async muscles here. We've got a few different async operations happening, fetching, parsing JSON, and we've returned all of that in an async function, which means we have an async value there, a promise that we need to await.
[00:02:40] Excellent work y'all. Any questions so far?
>> Just to clarify.
>> When we use the function, we have to say await name function.
>> Well, we don't have to, but the thing is that similar to before when we called fetch for the first time, if we call our fetchMessage function because we declared it with async function.
[00:03:01] If I call fetchMessage, what I get back is a promise. So we don't have to use await, but if we wanna do anything with the value, we're gonna need await. So that is the difference there. Good question. Okay, yes.