• Python@programming.dev
    link
    fedilink
    arrow-up
    12
    arrow-down
    3
    ·
    8 months ago

    I’ll do you one better

    function* sorry() { yield "I'm sorry"; }

    Call sorry.next().value as many times as you need to baby, hell you can even use it in a for-of loop because Generator functions are Iterable. I fucking love JavaScript

    • brian@programming.dev
      link
      fedilink
      arrow-up
      5
      ·
      8 months ago

      lol that doesn’t work either tho. it yields the string once and then is done. you still need a loop inside

      • Python@programming.dev
        link
        fedilink
        arrow-up
        1
        ·
        7 months ago

        You’re right! That’s actually a really cool point about Generator functions too, them having while(true) loops is very unproblematic :D

        I’m correcting myself to:

        function* sorry() { while(true){ yield "I'm sorry"; } }