Question

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?

Was it helpful?

Solution

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"

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top