<script>
function *gen(x){
yield x+2;
yield 7;
return x;
}
/* generator函数遇到yield关键字 函数会暂停 yield将函数切割了
next()方法会返回一个对象
*/
var g = gen(3);
console.log(g.next())//{value: 5, done: false}
console.log(g.next()) //{value:7,done:false}
console.log(g.next()) //{value: 3, done: true}
</script>
<script>
function *go(x){
var y = yield x+2;
console.log(y) //7
var z = yield y+10;
console.log(z); //8
return 4;
}
var obj = go(3);
console.log(obj.next()) //{value: 5, done: false}
console.log(obj.next(7)) //{value: 17, done: false}
console.log(obj.next(8)) //{value: 4, done: true}
</script>
<script>
var baseUrl = "http://192.168.14.49:5000/"
function http(url){
return $.ajax({
url:baseUrl+url,
dataType:"json"
})
}
function *getData(){
var id = yield http("top/playlist");
console.log(id);
yield http(`playlist/detail?id=${id}`);
}
var res = getData();
res.next().value.then(data=>{
var {id,name} = data.playlists[0];
res.next(id).value.then(res=>{
console.log(res)
})
})
</script>