Powered by md-Blog  文 - 篇  访客 -

基于ES6生成器generator制作的一个ajax请求工作流


  分类:写一个Demo  / 
更新:2020-03-01 09:20:51  /  创建:2020-03-01 09:20:51
不要删除
  • step_2 的请求数据依赖于 step_1
  • step_3 的请求数据依赖于 step_2
<!DOCTYPE html>
<html lang="ch">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Demo</title>
</head>
<body>
</body>
</html>

<script src="https://cdn.bootcss.com/axios/0.19.2/axios.min.js"></script>

<script>
function ajax(url) {
    axios.get(url).then(res => userGen.next(res.data))
}

function* steps() {
    console.log('step_1');
    const users = yield ajax('https://api.github.com/users');
    console.log(users);

    console.log('step_2');
    const firstUser = yield ajax(`https://api.github.com/users/${users[0].login}`);
    console.log(firstUser);

    console.log('step_3');
    const followers = yield ajax(firstUser.followers_url)
    console.log(followers);
}

const userGen = steps();
userGen.next();
</script>

不要删除

小站不易,感谢支持!