Javascript generators in Scala.js
<p>Currently I use Transcrypt to generate Javascript code from Python code. In this way I'm able to implement generators in Python like:</p>

<pre><code>def color():
    colors = ["red", "blue", "yellow"]
    i = -1
    while True:
        i += 1
        if i &gt;= colors.length:
            i = 0
        reset = yield colors[i]
        if reset:
            i = -1

gen = color()
console.log(next(gen)) # red
console.log(gen.js_next(True).value) # red
console.log(next(gen)) # blue
console.log(next(gen)) # yellow
console.log(next(gen)) # red
</code></pre>

<p>which will be compiled to Javascript like:</p>

<pre><code>var color = function* () {
    var colors = list (['red', 'blue', 'yellow']);
    var i = -(1);
    while (true) {
        i++;
        if (i &gt;= colors.length) {
            var i = 0;
        }
        var reset = yield colors [i];
        if (reset) {
            var i = -(1);
        }
    }
};
var gen = color ();
console.log (py_next (gen));
console.log (gen.next (true).value);
console.log (py_next (gen));
console.log (py_next (gen));
console.log (py_next (gen));
</code></pre>

<p>But since I have also Scala knowledge (and an Scala-application which I would like to implement in the browser) I'm looking to Scala.js. But as far as I know this generator construct is not possible in Scala, and the corresponding <code>yield</code> keyword is used in a different way.</p>

<p>Is the generator syntax possible in Scala.js, or am I forced to use Python and Transcrypt if I want this?</p>

<p>I believe the general concept you are looking for is Continuations. It's a fairly large and complex topic unto itself -- they used to be talked about more, but have largely been supplanted by the easier-to-use async library. But the <a href="https://index.scala-lang.org/scala/scala-continuations" rel="nofollow">scala-continuations library</a> is still around, and discussed various places online -- for example, <a href="https://blog.knoldus.com/2016/05/04/time-travel-in-scala-cps-in-scala-scalas-continuation/" rel="nofollow">this article</a>, or <a href="https://medium.com/@anicolaspp/c-yield-return-and-scala-continuations-d2a6917aa6d4#.1zeftpiue" rel="nofollow">this one</a>.</p>

javascript python scala scala.js transcrypt