sane-generator
Why do I need Sane Generator?
In short, ECMAScript 6 Generators are closed if we break out of the loop which iterates them. I believe that this limits the Generators. For example, in Python, the following would work
>>> numbers = (num for num in )>>> for num in numbers:... if num == 3:... break...>>> 4
This allows us partially consume the Generators whenever we want and that allows us to have elegant solutions like this one, by Jon Clements.
But the similar code in ECMAScript 6 would close the generator.
{ for var i = 0; i < 10; i += 1 i; } var numbers = ; for var num of numbers if num === 3 break; console;// {"done": true}
This module provides a way to overcome this feature.
I explained in detail in this blog post http://www.thefourtheye.in/2016/02/sane-ecmascript-6-generators.html.
Installation
npm install sane-generator
Usage
var SaneGenerator = ; { for var i = 0; i < 10; i += 1 i; } var numbers = ; for var num of numbers if num === 3 break; console;// {"value": 4, "done": false}
How to explicitly close the SaneGenerator?
var SaneGenerator = ; { for var i = 0; i < 10; i += 1 i; } var numbers = ; console;// {"value": 0, "done": false}console;// {"value": 0, "done": false}console; // call `return` with any value// {"done": true}console;// {"value": undefined, "done": true}