ES6 从入门到精通 # 16:Generator 的应用

简介: ES6 从入门到精通 # 16:Generator 的应用

说明

ES6 从入门到精通系列(全23讲)学习笔记。



解决回调地狱

比如有个需求,需要通过接口 https://v0.yiketianqi.com/api?unescape=1&version=v91&appid=43656176&appsecret=I42og6Lm&ext=&cityid=&city= 先获取北京的天气,然后获取上海的,最后获取广州的。


<!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="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script>
</head>
<body>
    <script>
        $.ajax({
            url: "https://v0.yiketianqi.com/api?unescape=1&version=v91&appid=43656176&appsecret=I42og6Lm&ext=&cityid=&city=北京",
            method: "get",
            success(res) {
                console.log("res", res);
                $.ajax({
                    url: "https://v0.yiketianqi.com/api?unescape=1&version=v91&appid=43656176&appsecret=I42og6Lm&ext=&cityid=&city=上海",
                    method: "get",
                    success(res1) {
                        console.log("res1", res1);
                        $.ajax({
                            url: "https://v0.yiketianqi.com/api?unescape=1&version=v91&appid=43656176&appsecret=I42og6Lm&ext=&cityid=&city=广州",
                            method: "get",
                            success(res2) {
                                console.log("res2", res2);
                                // ...
                            }
                        })
                    }
                })
            }
        })
    </script>    
</body>
</html>

bd263b5a1c264b90a943cdef08301353.png


上面就是回调地狱的展示,下面使用 Generator 处理,Generator 能让异步代码同步化

<!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="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script>
</head>
<body>
    <script>
        function* main() {
            let res = yield getData("北京");
            console.log("res", res);
            let res1 = yield getData("上海");
            console.log("res1", res1);
            let res2 = yield getData("广州");
            console.log("res2", res2);
        }
        const ite = main();
        ite.next();
        function getData(city) {
            $.ajax({
                url: `https://v0.yiketianqi.com/api?unescape=1&version=v91&appid=43656176&appsecret=I42og6Lm&ext=&cityid=&city=${city}`,
                method: "get",
                success(res) {
                    ite.next(res);
                }
            })
        }
    </script>    
</body>
</html>


6fe3bf8436154e848a46431c8607f2a2.png


其他例子:加载效果的异步

function loadUI() {
  console.log("加载----loading")
}
function getData() {
    setTimeout(() => {
        console.log("获取数据----data")
    }, 1000) 
}
function hideUI() {
    console.log("隐藏----loading")
}
loadUI();
getData();
hideUI();


d2e6010c3f274eaa8170ceb139efccf9.png


下面使用 Generator 处理

function* loadMain() {
    loadUI();
    yield getData();
    hideUI();
}
function loadUI() {
    console.log("加载----loading")
}
function getData() {
    setTimeout(() => {
        console.log("获取数据----data")
        ite.next();
    }, 1000) 
}
function hideUI() {
    console.log("隐藏----loading")
}
const ite = loadMain();
ite.next();


5991ddc1e71b4e95989e5e9ee861299a.png


目录
相关文章
|
5月前
ES6 Generator 函数
ES6 Generator 函数
|
4月前
|
JavaScript 前端开发 Java
ES6 简介
ES6 简介
|
5月前
ES6学习(九)—Generator 函数的语法
ES6学习(九)—Generator 函数的语法
|
11月前
|
缓存 JavaScript 前端开发
「万字进阶」深入浅出 Commonjs 和 Es Module
全方位介绍模块化 Commonjs 和 Es Module
「万字进阶」深入浅出 Commonjs 和 Es Module
|
11月前
|
前端开发
【ES6新特性】— Generator
【ES6新特性】— Generator
58 0
ES6 从入门到精通 # 15:生成器 Generator 的用法
ES6 从入门到精通 # 15:生成器 Generator 的用法
73 0
ES6 从入门到精通 # 15:生成器 Generator 的用法
|
前端开发
ES6 从入门到精通 # 20:async 的用法
ES6 从入门到精通 # 20:async 的用法
79 0
ES6 从入门到精通 # 20:async 的用法
|
JavaScript 前端开发 编译器
ES6 从入门到精通 # 01:ES6 介绍
ES6 从入门到精通 # 01:ES6 介绍
87 0
ES6 从入门到精通 # 01:ES6 介绍
|
前端开发 容器
ES6 从入门到精通 # 17:Promise 的基本使用
ES6 从入门到精通 # 17:Promise 的基本使用
70 0
ES6 从入门到精通 # 17:Promise 的基本使用
|
JavaScript 前端开发 Java
【ES6】Generator函数详解
【ES6】Generator函数详解
111 0