Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strange yield syntax in JavaScript

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?

like image 439
Matthew Gertner Avatar asked Aug 11 '11 16:08

Matthew Gertner


1 Answers

Actually, the relevant paragraph is a little below in https://developer.mozilla.org/En/New_in_JavaScript_1.7:

Once a generator has been started by calling its next() method, you can use send(), passing a specific value that will be treated as the result of the last yield. The generator will then return the operand of the subsequent yield.

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]);
like image 159
Wladimir Palant Avatar answered Sep 19 '22 09:09

Wladimir Palant