concept next method in category javascript

appears as: next method, next method, The next method
Secrets of the JavaScript Ninja, Second Edition

This is an excerpt from Manning's book Secrets of the JavaScript Ninja, Second Edition.

In this case, the first call to the iterator’s next method executes the generator code to the first yield expression, yield "Katana", and returns an object with the property value set to Katana and the property done set to false, signaling that there are more values to produce.

Another interesting thing happens when we call the next method on the iterator:

const result1 = ninjaIterator.next();

If this was a standard straightforward function call, this would cause the creation of a new next() execution context item, which would be placed on the stack. But as you might have noticed, generators are anything but standard, and a call to the next method of an iterator behaves a lot differently. It reactivates the matching execution context, in this case, the NinjaGenerator context, and places it on top of the stack, continuing the execution where it left off, as shown in figure 6.8.

Figure 6.8. Calling the iterator’s next method reactivates the execution context stack item of the matching generator, pushes it on the stack, and continues where it left off the last time.
Listing 6.8. Sending data to and receiving data from a generator
sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage
test yourself with a liveTest