this value inside anonymous generator

Jordan Harband ljharb at gmail.com
Tue Mar 31 06:46:20 UTC 2015


Can you not just use .bind here?

```
spawn(function* () {
  let data = yield this.fetchData();
}.bind(this));
```

On Mon, Mar 30, 2015 at 11:38 PM, Niloy Mondal <niloy.mondal84 at gmail.com>
wrote:

> 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.
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20150330/7b0bd819/attachment.html>


More information about the es-discuss mailing list