一.co
generator必须要结合co这个库来使用
引入cohttps://www.bootcdn.cn/co/
<script src="https://cdn.bootcss.com/co/4.1.0/index.js"></script>
<script>
/*generator 必须要结合co这个库来用 */
async function go(x){
return x;
}
co(function *(){
var data=yield go(3);
console.log(data); //3
var y=yield go(4);
console.log(y) //4
})
</script>
二.http-generator
<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></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>
三.http-generator-co
<script src="https://cdn.bootcss.com/co/4.1.0/index.js"></script>
<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script>
<script>
var baseUrl="http://192.168.14.49:5000/"
function http(url){
return $.ajax({
url:baseUrl+url,
dataType:"json"
})
}
co(function *(){
const data=yield http("top/playlist");
var {id,name}=data.playlists[0];
const detail=yield http(`playlist/detail?id=${id}`)
console.log(detail)
})
</script>