The problem I mentioned is part of a feature that’s not that important here, so I’ll try and just outline the generator part. Please assume there’s a good reason for doing this in practice.
Imagine you have an array of strings, where each string is some CoffeeScript. For a simple example,
["a=1", "console.log a"].
There’s a need to compile and evaluate each string in order. Each evaluation must be in the same namespace. The evaluation must not be global. The evaluation must have access to the global scope and the DOM [so workers are out].
The obvious thing to do would be to create a function that iterates over the array, compiling and evaluating each string locally. The problem is that the user needs to be able to run the first string, then do other stuff, then whenever they’re ready, run the next string. Using generators, the function could be a simple co-routine that yields
undefined after each evaluation, and the UI would call the generator’s
next method to run the next string.
Without a generator, it’s difficult to see how you could keep the namespace intact, while indefinitely yielding execution to the main thread on each iteration.
Again, I’m not saying it can’t be done, or that this is a commonplace problem, just that there are times in a single threaded, graphical environment where co-routines are required, or at least very difficult to live without.