How to use yield to pause and resume a generator function* in JavaScript

1 Answer

0 votes
function* f(i) {
  while (i < 3) {
    yield i;
    i++;
  }
}

const iterator = f(1);

console.log(iterator.next().value); // 1

console.log(iterator.next().value); // 2


  
    
    
/*
run:
    
1
2
    
*/

  

 



answered Nov 21, 2020 by avibootz

Related questions

...