Generator实例,维护多个ajax依次执行的队列
Generator实例,维护多个ajax依次执行的队列
ps 需要提前引入<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
// Generator实例,维护多个ajax依次执行的队列 function ajax(url){ axios.get(url).then(res => userGen.next(res.data)) } function *steps(){ console.log('fetching users'); const users = yield ajax('https://api.github.com/users'); console.log(users); console.log('fetching firstUser'); const firstUser = yield ajax(`https://api.github.com/users/${users[0].login}`); console.log(firstUser); console.log('fetching followers'); const followers = yield ajax(firstUser.followers_url); console.log(followers); } const userGen = steps(); userGen.next();
...