0

I am a bit confused about when a value is yielded in JS 1.7's new yield feature.

When I write my function like this:

function helloWorld() {
    console.log('hello'); 
    yield "world";
} 

var sayHello = helloWorld();

sayHello.next();

It returns:

>"world"
>"hello"

But when I write my function like this:

function helloWorld() {
    console.log('hello'); 
    yield console.log("world");
}

var sayHello = helloWorld();

sayHello.next();

It returns:

>"hello"
>"world"

as I would expect.

I am confused as to why in the first case "world" returns before "hello", perhaps I do not understand how yield works, could anyone who might know elaborate on why yield behaves this way?

Derek
  • 13
  • 3
  • 2
    please look here http://stackoverflow.com/questions/2282140/whats-the-yield-keyword-in-javascript that may answers your question. – Christos Mar 26 '14 at 10:35
  • Ahh I did read this first when I started playing with yield statements, this compares a yield with a return essentially. But if i replaced the world yield with return it would still log "hello" before returning "world". I am wondering why when calling next the yield value returns before "world" in the case it's a primitive? I am pretty familiar with generators and yielding in python, but this seems strange in the new JS implementation. – Derek Mar 27 '14 at 00:19

1 Answers1

0

Sorry guy's this seems to be an error.

I tried using yield using the Firefox console and it outputted this, but after I actually constructed an actual file and ran it though firefox it behaved as expected

If anyone is curious

<html>
<head>
</head>
<body>
<script type="application/javascript;version=1.7" >
function helloWorld () {
    console.log("hello"); 
    yield "world";
    yield "END";
} 

var sayHello = helloWorld();

console.log(sayHello.next());

function helloWorld1 () {
    console.log('hello'); 
    yield console.log("world");
    yield "END2";
}

var sayHello1 = helloWorld1();

sayHello1.next();
console.log(sayHello.next());
console.log(sayHello1.next());
</script>
</body>
</html>

outputs:

"hello" "world" "hello" "world" "END" "END2"

Derek
  • 13
  • 3