Use of ES6 generator function

Time:2021-9-22

understand

  1. The JavaScript generator executes the current yield code only when it calls next()
  2. Yield can only appear in the generator function, otherwise an error will be reported
  3. If the yield expression is used in another expression, it must be placed in parentheses. Console.log (‘Hello ‘+ (yield 123))
  4. The next method can take a parameter, which will be treated as the return value of the previous yield expression
  5. The for… Of loop can automatically traverse the iterator object generated when the generator function runs, and there is no need to call the next method at this time

reference material

  1. https://blog.csdn.net/qq_4500…
  2. https://www.runoob.com/w3cnot…