Transcript from the "Async Iteration" Lesson
>> Kyle Simpson: Now, there are some issues with async functions. And you might've seen, for example, in that exercise that we just did, that if you tried to run a function and pass it to forEach, like I'm doing here. And then, inside of it, you wanted to call the await keyword, you might have noticed that this would fail.
[00:00:23] And the reason it fails is because the await keyword has to be used inside of an async function, not inside of a regular function. So what do we do whenever we want to do this sort of deep awaiting, if you will, when we have a function boundary? You might think, okay, well, my solution is, I just need to pass the async function to the forEach method.
[00:00:48] Except the problem with that is that async functions come back with promises, and the forEach method does not know what to do with a promise. It does not know how to wait on promises to finish. So you'd get back an iteration over a bunch of promises, but they wouldn't be waiting on each other, and they would not be printing the contents of this file in the correct order.
[00:01:12] They would just print them as fast as those three separate promises resolved. So what we're really getting at here is that there's actually a conceptually missing piece to the puzzle. Not just that the syntax isn't what we want, but there's a missing piece to the puzzle. And it is that what we really need is an asynchronous iterator.
[00:02:07] And they don't know that they should pause if they find a promise. So what we need is a lazy asynchronous iterator that can pause whenever it sees, I'm sorry, we need an eager asynchronous iterator, which means it tries to go to a promise. It tries to go to each value, but if it sees a promise, then it moves on, then it waits before it moves on.
[00:03:28] You notice now that I can pass an async function directly into FA.serial.forEach, because the async function, when it gives a promise, FA.serial.forEach knows what to do with that promise. Whereas the normal built-in forEach would not know what to do.