I just took a look at Dave Herman’s very interesting task.js. In his example he has this line:
var [foo, bar] = yield join(read("foo.json"),
read("bar.json")).timeout(1000);
I’m familiar with generators but I don’t understand how the yield expression evaluates to something that can be assigned to [foo, bar]. I actually wouldn’t have expected the expression to be assignable to anything since it is basically the same thing as return.
The yield syntax for JS still seems a bit underdocumented and I couldn’t find info about this.
So to clarify my question: what ends up being assigned to foo and bar?
Actually, the relevant paragraph is a little below in https://developer.mozilla.org/En/New_in_JavaScript_1.7:
I think that this is only relevant if the generator is used by calling its methods directly, not when looping over its values – a loop will always call
next()on the generator and neversend().In a way, generator execution is similar to cooperative multitasking. The generator executes until the
yieldstatement is found. It returns control to whoever callednext()orsend()on the generator. The caller then continues executing, until the nextnext()orsend()call is performed – now the generator is executing again. Each time values can be passed back and forth.Here a simple example: