UNPKG

myplayaaaaa

Version:

wwww

40 lines 1.46 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script src="js/axios.js"></script> </head> <body> <script> // "https://jsonplaceholder.typicode.com/posts" // "https://jsonplaceholder.typicode.com/users" // 创建一个generator函数 他的作用是 调用他时返回一个遍历器对象 // 可以用来遍历 array ret map ...等数据类型 function *gen(){ var prost= yield axios.get("https://jsonplaceholder.typicode.com/posts") console.log("prost",prost); var datas = yield axios.get("https://jsonplaceholder.typicode.com/users") console.log("datas",datas); } // 创建哈拿书用来接收到要遍历的数据并调用generator函数返回遍历器后进行遍历 function run(generator){ var myGen=generator() // 创建一个函数用来封装遍历yield function handle(yielded){ // 接受到数据进行遍历 if(!yielded.done){ yielded.value.then(function(data){ console.log( yielded.value); return handle(myGen.next(data)) }) } } // 调用handle函数将.next()的返回值{value:数据 done:false} 传给他 return handle(myGen.next())// {value:promise done:false} } </script> </body> </html>