How to check if Javascript generator has exited?

1.4k views Asked by At

I'm trying to implement a generator in Javascript and trying to determine whether or not my generator has completed.

There is a lot of conflicting information out there (I suppose since this is still a new feature). Anyhow, I'm running this using Rhino 1.7 release 4 from within Java. This is the syntax which seems to work:

function poem(foo)
{
    console.println("I think that I shall never see");
    var a = yield(foo);
    console.println("A poem as lovely as a tree");
    var b = yield(foo);
    console.println("And so I pushed it into the sea");
    var c = yield(foo);
    console.println("Hee hee hee hee");
}

var gen = poem();

gen.next();
console.println("is done " + gen.next(5).done);

This is different from the official documentation on MDN and various other websites. (The 'function*' keyword does not seem to work, and yield seems to expect parenthesis.) I was also expecting the next() function to return an object with a 'value' and 'done' member, but I am getting 'undefined' instead.

I could catch the exception that is thrown when next() is called after the generator is finished, but this seems inelegant. I also want to be more explicit that putting it inside a for() clause and having Javascript do the checking for me.

Is there a better way to check for when my generator finishes?

1

There are 1 answers

2
Rob W On BEST ANSWER

function* is the syntax for generators as defined in ECMAScript 6, which is a recent feature.

Rhino 1.7 is ancient, and also supports "generators", but its syntax is non-standard (back then, the function* syntax did not even exist!). You can find documentation for this old syntax at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Iterators_and_Generators#Generators.3A_a_better_way_to_build_Iterators. When this old generator completes, an StopIteration exception is thrown. So you have to use try-catch to detect generator termination.

function legacyGenerator() {
    yield 1;
}
var gen = legacyGenerator();
gen.next(); // 1
gen.next(); // throws StopIteration