this value inside anonymous generator

Niloy Mondal niloy.mondal84 at gmail.com
Tue Mar 31 06:38:38 UTC 2015


I wrote some code like the following in tracuer

```
class Foo {
  constructor() {
    spawn(function*() {
      let data = yield this.fetchData(); // error because 'this' is
undefined
    });
  }

  fetchData() {
    // returns promise
  }
}
```

I used the spawn function from here:
https://gist.github.com/jakearchibald/31b89cba627924972ad6

The code doesn't work because 'this' is undefined. I can fix it by using
'let that = this' pattern. But it feels like a step backward now that I've
fallen in love with lambda functions. So I was wondering if it would be
possible to use lexical binding for 'this' inside anonymous generators.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20150331/66846bca/attachment.html>


More information about the es-discuss mailing list