Js generator iterator

clipboard.png
6.5

clipboard.png
comment a b (). Next () is followed by a 6.5. Why does the second next () cause console.log (6.5) to be executed again? How to understand this problem?

Apr.01,2022

try changing the timer to an ordinary function


each call to the generator function generates a new iterator . You call b () twice, so you generate two new iterators, each of which is unassociated and executes independently. You may mean that you want to execute the same iterator twice instead of generating two iterators, both once:

function a () {
  function * b () {
    yield setTimeout(() => {
      console.log(6.5)
    }, 5000)
    yield 2
  }
  let iterator = b()
  iterator.next()
  iterator.next()
}
a()

clipboard.png

Menu