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?
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?
Share Improve this question edited Aug 20, 2013 at 9:30 Dheeraj Vepakomma 28.9k18 gold badges85 silver badges107 bronze badges asked Aug 11, 2011 at 16:47 Matthew GertnerMatthew Gertner 4,5372 gold badges35 silver badges54 bronze badges 2- BTW I wanted to add a "taskjs" keyword but it seems I don't have enough reputation for this. – Matthew Gertner Commented Aug 11, 2011 at 16:48
- 1 Please remember to ask a question :-) – driis Commented Aug 11, 2011 at 16:50
2 Answers
Reset to default 6Actually, the relevant paragraph is a little below in https://developer.mozilla/En/New_in_JavaScript_1.7:
Once a generator has been started by calling its
next()
method, you can usesend()
, passing a specific value that will be treated as the result of the lastyield
. The generator will then return the operand of the subsequentyield
.
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 never send()
.
In a way, generator execution is similar to cooperative multitasking. The generator executes until the yield
statement is found. It returns control to whoever called next()
or send()
on the generator. The caller then continues executing, until the next next()
or send()
call is performed - now the generator is executing again. Each time values can be passed back and forth.
Here a simple example:
function gen()
{
var [foo, bar] = yield 1;
console.log("Generator got: " + foo + ", " + bar);
}
// This creates a generator but doesn't run it yet
var g = gen();
// Starts generator execution until a yield statement returns a value
var result = g.next()
console.log("Received from generator: " + result);
// Continue generator execution with [2, 3] being the return value
// of the yield statement. This will throw StopIteration because the
// iterator doesn't have any more yield statements.
g.send([2, 3]);
https://developer.mozilla/En/New_in_JavaScript_1.7
The function containing the yield keyword is a generator. When you call it, its formal parameters are bound to actual arguments, but its body isn't actually evaluated. Instead, a generator-iterator is returned. Each call to the generator-iterator's next() method performs another pass through the iterative algorithm. Each step's value is the value specified by the yield keyword. Think of yield as the generator-iterator version of return, indicating the boundary between each iteration of the algorithm. Each time you call next(), the generator code resumes from the statement following the yield.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745259173a4619130.html
评论列表(0条)