title: How do I implement a cycle-through array with a generator function
body: <p>Today I was wondering what would be the swiftest method to provide a cycle-through array in TypeScript, as in:</p>
  
  <pre><code>['one', 'two', 'three'] 
  </code></pre>
  
  <p>where the next value after <code>three</code> would be <code>one</code>, and I thought that it's a good candidate for a generator function. However it does not seem to work for me. What's wrong with the following code?</p>
  
  <pre><code>function* stepGen(){
    const steps = ['one', 'two', 'three'];
  
    let index = 0;
  
    if(index &lt; steps.length - 1){
     index++;
    } else {
     index = 0;
    }
    yield steps[index];
  }
  
  let gen = stepGen();
  console.log(gen.next().value); 
  console.log(gen.next().value);
  console.log(gen.next().value); // should be 'three'
  console.log(gen.next().value); // should be 'one'
  console.log(gen.next().value);
  </code></pre>