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