Generators are a special type of function that can be paused and resumed. They are created using the function* keyword.
Generators are useful for tasks that need to be performed repeatedly, such as iterating over a large data set. They can also be used to implement asynchronous code, such as code that makes HTTP requests.
To use a generator, you first need to create it. You can do this using the function* keyword.
Once you have created a generator, you can start iterating over it using the next() method. The next() method will return the next value from the generator, or undefined if there are no more values.
You can also use the yield keyword to yield a value from the generator. The yield keyword will pause the generator and return the value to the caller. The generator will then resume at the next call to next().
Here is an example of a generator that iterates over a list of numbers:
Code snippet
function* generateNumbers() {
for (let i = 0; i < 10; i++) {
yield i;
}
}
const numbers = generateNumbers();
for (let number of numbers) {
console.log(number);
}
This code will print the numbers 0 to 9.
Generators can also be used to implement asynchronous code. Here is an example of a generator that makes an HTTP request:
This code will make an HTTP request to the GitHub API and print the user information for the user with the username "bard".
Generators are a powerful tool that can be used to improve the performance and readability of your code. They are especially useful for tasks that need to be performed repeatedly or for implementing asynchronous code.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Generators are a special type of function that can be paused and resumed. They are created using the function* keyword.
Generators are useful for tasks that need to be performed repeatedly, such as iterating over a large data set. They can also be used to implement asynchronous code, such as code that makes HTTP requests.
To use a generator, you first need to create it. You can do this using the function* keyword.
Once you have created a generator, you can start iterating over it using the next() method. The next() method will return the next value from the generator, or undefined if there are no more values.
You can also use the yield keyword to yield a value from the generator. The yield keyword will pause the generator and return the value to the caller. The generator will then resume at the next call to next().
Here is an example of a generator that iterates over a list of numbers:
Code snippet
This code will print the numbers 0 to 9.
Generators can also be used to implement asynchronous code. Here is an example of a generator that makes an HTTP request:
Code snippet
This code will make an HTTP request to the GitHub API and print the user information for the user with the username "bard".
Generators are a powerful tool that can be used to improve the performance and readability of your code. They are especially useful for tasks that need to be performed repeatedly or for implementing asynchronous code.