cujojs / when

A solid, fast Promises/A+ and when() implementation, plus other async goodies.
Other
3.44k stars 396 forks source link

generator has a syntax error on line 77 #429

Closed jandppw closed 9 years ago

jandppw commented 9 years ago
function error(e) {
    /*jshint validthis:true*/
    try {
        return handle(this.throw(e), this);
    } catch(e) {
        return reject(e);
    }
}

this.throw(e) cannot be right. I have no idea what the intention is, but

briancavalier commented 9 years ago

@jandppw This is indeed valid code in ES6, which is the only place generators are usable. Did you try running a generator with when/generate? It works quite well :)

Specifically:

The this is a generator iterator. The code arranges for the error function to be called with the proper thisArg--specifically when.js's internal chain method being used on that line will arrange for iterator to be the thisArg when either next or error is called.

All ES6 generator iterators have a throw method which sends an error back into the suspended generator.

ES5 allows keywords as property names, thus throw is a valid property name in all >= ES5 code. Since generators are ES6 only, throw is a valid property name anywhere you'd be using when/generate.

Generators and Promises are awesome together. Hope that helps.

jandppw commented 9 years ago

Brain,

Thx for the detailed answer!

The report was triggered by an error reported by the Closure compiler, trying to do a dojo build that includes wire, and thus when and meld.

A workaround was to mark the generator as copyOnly.

Sadly, the build doesn't work, but that's a different issue ...