Ai
1 Star 0 Fork 0

zhengmin/ES6

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
async.html 2.02 KB
一键复制 编辑 原始数据 按行查看 历史
zhengmin 提交于 2022-04-02 21:27 +08:00 . es6
<!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="./jquery-3.6.0.min.js"></script>
</head>
<body>
<script>
// 把一个异步的操作变成一个同步的操作,但是实质上还是异步
// function ajax(url){
// $.getJSON(url,function(data){
// info.next(data)
// })
// }
// function* getInfo(){
// var ids = yield ajax("http://iwenwiki.com/api/generator/list.php");
// var names = yield ajax("http://iwenwiki.com/api/generator/id.php?id="+ids[0]);
// var infos = yield ajax("http://iwenwiki.com/api/generator/name.php?name="+names.name)
// console.log(infos);
// }
// var info = getInfo();
// info.next()
// 接下来我们要写的才是日后我们真正要写的代码业务
function ajax(url) {
return new Promise((resolve, reject) => {
$.getJSON(url, function (data) {
resolve(data)
}, function (error) {
reject(error)
})
})
}
async function getInfo() {
var ids = await ajax("http://iwenwiki.com/api/generator/list.php")
var names = await ajax("http://iwenwiki.com/api/generator/id.php?id=" + ids[0])
var infos = await ajax("http://iwenwiki.com/api/generator/name.php?name=" + names.name)
console.log(infos);
}
getInfo();
// promise: 优势在于then,解决了回调函数问题
// Generator: 让异步以同步的方式处理
// async: 让异步以同步的方式处理,优势,代码的可读性更好了
// async + promise得到更优质的开发体验
// 回调函数 事件处理 Promise Generator async
</script>
</body>
</html>
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/wzm_love_coding/es6.git
git@gitee.com:wzm_love_coding/es6.git
wzm_love_coding
es6
ES6
master

搜索帮助