ES6学习笔记16 Genertor 抽奖和长轮询

{
let draw=function (count) {
// 具体抽奖逻辑
console.log(`剩余${count}次`)
}

let residue=function* (count){
while (count>0) {
count--
yield draw(count)
}
}

let star=residue(5)

let btn=document.createElement('button')
btn.id='start'
btn.textContent='抽奖'
document.body.appendChild(btn)
document.getElementById('start').addEventListener('click',function(){
star.next()
},false)
}

{
// 长轮询
let ajax=function* (){
yield new Promise(function(resolve,reject){
setTimeout(function(){
resolve({code:0})
},200)
})
}

let pull=function(){
let genertaor=ajax()
let step=genertaor.next()
step.value.then(function(d){
if(d.code!=0){
console.info('wait')
pull()
}else{
console.info(d)
}
})
}

pull()
}

猜你喜欢

转载自www.cnblogs.com/laohange/p/12815474.html