认识
一、认识
Generator
函数是 ES6
提供的一种异步编程解决方案,语法行为与传统函数完全不同。Generator
函数将 JavaScript
异步编程带入了一个全新的阶段。
二、语法
function* genFun() {
yield 1;
yield 2;
return 3;
}
const gen = genFun();
const result1 = gen.next();
console.log(result1); //{ value: 1, done: false }
const result2 = gen.next();
console.log(result2); // { value: 2, done: false }
const result3 = gen.next();
console.log(result3); // { value: 3, done: true }