Given the provided (very simple) generator, is it possible to return the generator back to its original state to use again?
var generator = function*() {
You can also have your generator reset your iterable like this:
let iterable = generator();
function* generator(){
yield 1;
yield 2;
yield 3;
iterable = generator();
}
for (let x of iterable) {
console.log(x);
}
//Now the generator has reset the iterable and the iterable is ready to go again.
for (let x of iterable) {
console.log(x);
}
I do not personally know the pros and cons of doing this. Just that it works as you would expect by reassigning the iterable each time the generator finishes.
EDIT: With more knowledge of how this work I would recommend just using the generator like Azder Showed:
const generator = function*(){
yield 1;
yield 2;
yield 3;
}
for (let x of generator()) {
console.log(x);
}
for (let x of generator()) {
console.log(x);
}
The version I recommended will prevent you from being able to run through the iterations if it ever fails... For example if you were waiting on one url to call another. If the first url fails you would have to refresh your app to before it would be able to try that first yield again.