JS Generator

function show() {
    console.log('a')
    console.log('b')
}
show() // 普通函数

function *show2() {
    console.log('1')
    yield
    console.log('2')
}
let genObj = show2()
genObj.next() // 1
genObj.next() // 2
genObj.next()

  

Need to call the next () method to start the implementation, we need to stop experiencing yield

plus a generator function front  * sides can have spaces, or near or functionfunction

If the function before missing *

  • It is a normal function
  • If there yieldwill be error, ReferenceError: yield is not defined
  • yield function can only be used inside Generator

Guess you like

Origin www.cnblogs.com/413xiaol/p/11129240.html