Check out a free preview of the full Advanced JavaScript course:
The "Quiz: Async Patterns" Lesson is part of the full, Advanced JavaScript course featured in this preview video. Here's what you'd learn in this lesson:

Quiz: Async Patterns

Get Unlimited Access Now

Transcript from the "Quiz: Async Patterns" Lesson

>> [MUSIC]

>> Kyle Simpson: So the bottom line takeaway is, you can use promises natively, or you can use any library that you're happy with. I think Asyncuence does a pretty good job of making it easy to deal with promises without all of the complexity, but feel free to use it or not.

[00:00:18] So, finishing up. What is callback hell? What is it really?
>> Speaker 1: Giving over control of your program.
>> Kyle Simpson: Exactly. It's inversion of control. It's handing your control of the rest of your program over some other utility and all of the trust that that involves. And all the problems it can introduce.

[00:00:39] The lack of error handling. The lack of retries the lack of knowing if it's going to do it too many times or not enough for the whole thing. How do you pause a generator? What was the mechanism we used for pausing a generator?
>> Speaker 1: Yield?
>> Kyle Simpson: Yield and how do you resume it?

>> Speaker 1: Next
>> Kyle Simpson: Next one considering. What is a promise?
>> Speaker 2: Something that will eventually either be rejected or accepted?
>> Kyle Simpson: Yeah. It's a future value is what it's usually called. It's a promise for a future value. It's a continuation of them. It's a transaction at Burger King, however you want to think about it metaphorically.

[00:01:18] And how does it solve the inversion of control issues?
>> Speaker 2: Give you back the ability to decide when you're code's gonna be run, or your continuation's gonna be run.
>> Kyle Simpson: That's right. Instead of passing my continuation in, I receive a promise back. So it uninverts that inversion of control.

[00:01:34] And it puts me back in control of deciding what step two looks like. Finally, how is it that we could, what was the key characteristic that allowed us to combine generators and promises for async flow control? Remember the generators yielding out promises. And the promise, when it completes, automatically restarting the generator.

[00:02:03] So, the key characteristic was call a generator, have it yield out a promise, and when that promise finishes have the promise restart the generator. And most of the async flow libraries out there, including asyncquence have a utility for that. Some of them call it spawn. Mine is called runner.

[00:02:19] But it automatically listens for yielded out promises and it restarts the generator when they complete.