در حال بارگذاری ...

Introducing Generators0:28

a function to exit at a particular point, or really more than exit, it allows a function to pause at a particular point. And then later you, the caller, have the ability to resume it, and then pause again potentially, and then resume it. So it's kind of an interesting thing, it's very different from what you might be used to. Now, we create a generator in two different steps. One, you begin the method name, or function name, with a star. Think of this as a little symbol that indicates, hey, this function is a generator, and it's going to return as you learn an iterator.

Using the yield Keyword0:54

Think of this as a little symbol that indicates, hey, this function is a generator, and it's going to return as you learn an iterator. So the 101 example you'll see often is something like this. yield 1, yield 2, yield 3, and let's leave it at that. Okay, so we see two brand new things here. The star indicates that the function is a generator, and then yield, brand new keyword to you. So this little expression here indicates that we are pausing and exiting early. So it's almost like a return statement, but not quite, because once you get to a return, it doesn't matter what comes below, right?

So it's almost like a return statement, but not quite, because once you get to a return, it doesn't matter what comes below, right? But with yield, we can cycle through all of these. So we could say a yield is very much like a return statement, but it can later resume from the exact same point using the exact same set of states. That's how I would think of it. So let's try it. But before we yield, I want to show you something. I'm going to console.log begin, just to signal that the body of the function has executed. All right, well, if we call numbers, you're going to see something pretty interesting.

Calling a Generator Function2:16

the function just like you've always done in the past. But because it's a generator, it's not going to execute these contents immediately. Instead, it's going to return a special type of object. So let's just return a call to numbers, and you're going to see, yeah, it looks a little different. We see all these references to generator status and generator function. That's our first indication that we're not just triggering the function and returning something. So that means how do I execute this? How do I get to this first yield?

Advancing with next()2:43

So that means how do I execute this? How do I get to this first yield? Okay, well, let's try this. We'll say let iterator or something like that equal a call to numbers. So right now, just like before, iterator is equal to our little generator object. But now if I say iterator.next, okay, well, now we begin the process. So we begin executing code, and then it pauses at the first yield statement, in this case 1. And sure enough, we can see value is 1 here. So that's the first thing to understand.

Reading value and done3:11

And sure enough, we can see value is 1 here. So that's the first thing to understand. The response from iterator.next is not just going to be the value you specify here or the string. It's going to be an object that contains the value, but also the status that indicates whether or not the generator is complete or done. So that means if you did want the value, you'd have to append .value to grab it. Okay, well, let's try this. Let's say console.log(iterator.next). I'm going to bring it back to how it was before.

Building a Range Generator4:32

But now we call iterator.next, so it starts back up and it goes to the next yield. And in this case, it's 2, so we get that one. So now we are paused here. And if we never called anything again, we wouldn't get beyond that point. But we call iterator.next again, and we once again continue execution to the next yield point where you get the value. Now let's review a slightly different example, but still fairly simple. We'll use a range function. So we're going to recreate a range functionality. So we want it to be a generator, so we start it with a *.

So we're going to recreate a range functionality. So we want it to be a generator, so we start it with a yield. You also might see this, by the way. It doesn't matter. Any convention you want to follow here is totally fine. This is what I've been doing lately, even though I see a good argument to be made for this. Okay. So the range is going to accept the start and where it should end. And now we could say yield start here, but this won't quite be what we want.

So the range is going to accept the start and where it should end. And now we could say yield start here, but this won't quite be what we want. So we'll say let iterator equals a call to range(1, 2, 5). Okay. We don't get anything. If we console.log(iterator), what are we going to get? We're going to get that generator status. So let's begin execution. So we yield start, so we get essentially this. But now we want to set it up so that every time we call iterator.next, we get the next.

This is kind of cool. We can set up a while statement, and actually we could set up one that never ends. So this would be if you want a range with no maximum ceiling, while true would actually be a way to accomplish that, which is cool. But in our case, we want to continue just as long as start is less than end. And then we will yield start, and let's see what we get here. Okay. So we start it up, we yield it, value is equal to 1, but then we run it again and value is still 1, which makes sense, right? So we paused and then we call next again.

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟