Reason why generators do not have references to themselves?

Brandon Benvie bbenvie at mozilla.com
Thu Jan 23 17:25:58 PST 2014


On 1/23/2014 4:46 PM, Domenic Denicola wrote:
> Task.js is still on "JavaScript 1.8," and is not ES6-compatible. It won't work with modern browsers, or with Regenerator.

For most uses, Task.js's Task.spawn can be replaced with a smaller 
helper function:

```js
function spawn(thunk) {
   return new Promise(function(resolve, reject) {
     var gen = thunk();

     function _next(v) {
       return handle(gen.next(v));
     }

     function _throw(e) {
       return handle(gen.throw(e));
     }

     function handle(r) {
       return r.done ? r.value : Promise.cast(r.value).then(_next, _throw);
     }

     Promise.cast(_next()).then(resolve, reject);
   });
}
```


More information about the es-discuss mailing list