JavaScript(JS) Generators
Create JavaScript Generators
In JavaScript, you can create a generator function using the function*
keyword. A generator function is a special type of function that can be paused and resumed, allowing you to generate a sequence of values on the fly.
Here is an example of how to create a generator function in JavaScript:
function* myGenerator() { yield 1; yield 2; yield 3; } let gen = myGenerator(); console.log(gen.next().value); // Output: 1 console.log(gen.next().value); // Output: 2 console.log(gen.next().value); // Output: 3
In this example, myGenerator
is a generator function that uses the yield
keyword to generate a sequence of values. When the generator is called, it returns an iterator object, which you can use to iterate over the generated values.
To generate each value, the yield
keyword is used to pause the generator and return a value to the calling code. When the generator is resumed, it continues executing from where it left off, allowing you to generate the next value in the sequence.
To call a generator function, you need to create an instance of the generator by calling it, as shown in the example above. This returns an iterator object, which you can use to iterate over the generated values using the next()
method.
The next()
method returns an object with two properties: value
, which is the generated value, and done
, which is a boolean indicating whether the generator has finished generating values.
You can also use the yield*
keyword to delegate to another generator or iterable, allowing you to compose generators together to create more complex sequences of values.
Using yield to Pause Execution
In JavaScript, the yield
keyword can be used inside a generator function to pause its execution and return a value to the caller. When the generator is resumed, execution continues from where it left off.
Here is an example of using the yield
keyword in a generator function:
function* myGenerator() { console.log('Starting generator...'); yield 1; console.log('Generator resumed...'); yield 2; console.log('Generator resumed...'); yield 3; console.log('Generator finished.'); } let gen = myGenerator(); console.log(gen.next().value); // Output: Starting generator... 1 console.log(gen.next().value); // Output: Generator resumed... 2 console.log(gen.next().value); // Output: Generator resumed... 3 console.log(gen.next().value); // Output: Generator finished. undefined
In this example, the myGenerator
function is a generator function that uses the yield
keyword to pause execution and return a value to the caller. When the generator is called, it returns an iterator object, which can be used to iterate over the values generated by the function.
Each time the yield
keyword is encountered, execution of the generator function is paused and the value specified after the yield
keyword is returned to the caller. The console.log
statements in the generator function are used to demonstrate the pausing and resuming of the function.
When the generator function is resumed using the next()
method of the iterator object, execution continues from where it left off, allowing you to generate the next value in the sequence.
Once there are no more values to generate, the done
property of the iterator object is set to true
, indicating that the generator has finished.
Working of multiple yield Statements
In JavaScript, a generator function can contain multiple yield
statements, allowing you to generate a sequence of values on the fly.
Here is an example of a generator function with multiple yield
statements:
function* myGenerator() { yield 'apple'; yield 'banana'; yield 'cherry'; } let gen = myGenerator(); console.log(gen.next().value); // Output: 'apple' console.log(gen.next().value); // Output: 'banana' console.log(gen.next().value); // Output: 'cherry' console.log(gen.next().value); // Output: undefined
In this example, the myGenerator
function is a generator function that contains three yield
statements, each returning a different value. When the generator is called, it returns an iterator object, which can be used to iterate over the values generated by the function.
When the first next()
method is called on the iterator object, the generator function is executed until the first yield
statement is reached. The value specified after the yield
keyword ('apple'
in this case) is returned to the caller.
Subsequent calls to next()
on the iterator object cause the generator function to resume execution from where it left off, generating the next value in the sequence.
When there are no more values to generate, the done
property of the iterator object is set to true
, indicating that the generator has finished.
Passing Arguments to Generator Functions
In JavaScript, you can pass arguments to a generator function when it is called, just like any other function.
Here is an example of passing arguments to a generator function:
function* myGenerator(start, end) { for (let i = start; i <= end; i++) { yield i; } } let gen = myGenerator(1, 3); console.log(gen.next().value); // Output: 1 console.log(gen.next().value); // Output: 2 console.log(gen.next().value); // Output: 3 console.log(gen.next().value); // Output: undefined
In this example, the myGenerator
function takes two arguments (start
and end
) and generates a sequence of numbers between those two values.
When the generator is called with myGenerator(1, 3)
, it returns an iterator object, which can be used to iterate over the values generated by the function.
The for
loop in the generator function generates the sequence of numbers between start
and end
, and the yield
keyword is used to pause execution and return each value to the caller.
Each time the next()
method is called on the iterator object, the generator function resumes execution from where it left off, generating the next value in the sequence.
When there are no more values to generate, the done
property of the iterator object is set to true
, indicating that the generator has finished.
Generator Methods
In JavaScript, generator methods are generator functions defined as methods on an object or class. They are similar to regular generator functions, but they are defined within the context of an object or class and are called on an instance of that object or class.
Here is an example of a generator method:
class MyObject { *myGenerator() { yield 'apple'; yield 'banana'; yield 'cherry'; } } let obj = new MyObject(); let gen = obj.myGenerator(); console.log(gen.next().value); // Output: 'apple' console.log(gen.next().value); // Output: 'banana' console.log(gen.next().value); // Output: 'cherry' console.log(gen.next().value); // Output: undefined
In this example, the MyObject
class has a generator method called myGenerator
. This generator method is defined using the *
syntax before the method name, which indicates that it is a generator function.
When an instance of MyObject
is created (let obj = new MyObject()
), the myGenerator
method can be called on that instance to
Generator Methods
In JavaScript, generator methods are functions that allow you to define an iterative algorithm by writing a single function that can be paused and resumed. They are created using the function*
syntax and use the yield
keyword to pause execution and return a value to the caller.
Here's an example of a simple generator method that returns an infinite sequence of even numbers:
function* generateEvenNumbers() { let num = 0; while (true) { yield num; num += 2; } }
In this example, the generateEvenNumbers
function uses a while
loop to generate an infinite sequence of even numbers. Each time the loop executes, it uses the yield
keyword to pause execution and return the current value of num
to the caller. The yield
keyword effectively turns the function into an iterator, allowing you to iterate over the sequence of even numbers using a for...of
loop, for example:
const evenNumbers = generateEvenNumbers(); for (let i = 0; i < 10; i++) { console.log(evenNumbers.next().value); // prints 0, 2, 4, 6, 8, 10, 12, 14, 16, 18 }
In this example, the generateEvenNumbers
function is called to create an iterator object, which is then used to iterate over the first 10 even numbers in the sequence.
Generator methods can be used for a variety of purposes, such as generating sequences of data, implementing lazy evaluation, and implementing asynchronous operations using the yield
keyword to pause execution until a result is available.