Cannot Eval js code with yield

231 views Asked by At

Here is my simplified code:

<html>
<body>
    <button onclick="testRun()">Click me</button>
    <script type="application/javascript" language='javascript1.7'>
        function testRun() {
            var txt = 'var func = function*() {console.log("hi");';
            txt += 'yield true;';
            txt += 'console.log("bye");';
            txt += 'yield false;}';
            eval(txt);
            func().next();
            func().next();
        }
    </script>
</body>
</html>

I keep getting the following error:

1.html:1 Uncaught SyntaxError: Unexpected token true

I have seen the following examples:

http://forum.unity3d.com/threads/eval-yield-waitforseconds-javascript.248463/

http://unixpapa.com/js/sleep.html

but haven't been able to do it myself.

can anyone direct me in the right direction?

thanks

edit: thanks for the quick answers but I have encountered a new issue: when using the func().next(); twice I keep getting "hi" "hi" instead of "hi" bye" can anyone help me understand what am i doing wrong?

1

There are 1 answers

0
G. Moore On

Your code will work if you change it to:

    function testRun() {
        var txt = 'function* func() {console.log("hi");';
        txt += 'yield true;';
        txt += 'console.log("bye");';
        txt += 'yield false;}';
        eval(txt);
        var gen=func();
        gen.next();
        gen.next();
    }