//案例:使用Generator函数模拟抽奖的实现
function choujiang() {
console.log("执行抽奖业务逻辑")
}
let myGenerator = function* () {
//控制抽奖次数
let count = 3
while (count > 0) {
choujiang()
count--
yield
if (count == 0) {
console.log("没有机会了")
} else {
console.log(`剩余${count}次机会`)
}
}
}
let mygenerator = myGenerator()
function onStart() {
mygenerator.next()
}
原文:https://www.cnblogs.com/lceihen/p/14454187.html