Check out a free preview of the full JavaScript: The Recent Parts course

The "Async Await Exercise" Lesson is part of the full, JavaScript: The Recent Parts course featured in this preview video. Here's what you'd learn in this lesson:

Students are instructed to construct an async function where files are given to the function by a mock AJAX call, and are logged in the order that they are received.

Preview
Close

Transcript from the "Async Await Exercise" Lesson

[00:00:00]
>> Kyle Simpson: So async functions they are amazing. They are so much better than promised Jane's. Let's get some practice using an async await syntax that synchronous async pattern so called. Let's get some practice with that. We're gonna be making fake ajax calls here. We're going to be requesting three separate files.

[00:00:20]
We want to request them concurrently, meaning we want to request them all at once but we don't want to print them ASAP, I mean we don't want to print them in any order. We want to make sure to print them in the specific order that they should be coming back.

[00:00:35]
So in other words, we want to print the response to file one and then the response to file two and then the response to file three. So we're requesting them concurrently, or printing them sequentially, but the last little twist is that we do wanna print them as soon as they come back.

[00:00:52]
So in other words, we want to if file one's response comes back right away print it even if we haven't yet gotten file two or file three. So, we can't just wait for all of them to finish for printing room. So, we're gonna be using promises here, that's what your get file utility that you'll call.

[00:01:12]
You'll call that with the thing that you're, the string that you're getting and it will give you a promise, it'll be awaiting that promise and then using that result to print it out, okay? Again, this is only about five to eight lines of code. So don't feel like you need to write a big, complex solution here.

[00:01:31]
Just think carefully about where your awaits need to be. Because an await does block the execution of the rest of the function. And you don't want to block things unnecessarily. If you need hint because you get stuck, feel free to take a look at the solution file ex.fixed.

[00:01:48]
We'll be back in a few minutes to talk about the solution to this exercise.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now