🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
[TOC] ## **什么是回调地狱** > 就是在一个回调函数里面嵌套一个回调函数,甚至嵌套更多个回调函数 [什么是回调函数](https://www.kancloud.cn/juan1996/interview/905078) ### **回调地狱的实例** ``` <body> <input /> <button>搜索一下</button> <h1></h1> <img src="" alt=""> <p></p> <script> var baseUrl = 'http://api.douban.com/v2/movie/' $("button").click(function () { var value = $("input").val(); $.ajax({ url: `${baseUrl}search?q=${value}&count=1`, dataType: "jsonp", success:res=>{ var title = res.title; var imgUrl = res.subjects[0].images.small; var id = res.subjects[0].id; $("h1").html(title); $("img").attr("src",imgUrl) $.ajax({ url:`${baseUrl}${id}`, dataType: "jsonp", success:res=>{ var summary = res.summary; $("p").html(summary) } }) } }) }) </script> </body> ``` ## **用回调函数封装回调地狱** ``` <body> <input /> <button>搜索一下</button> <h1></h1> <img src="" alt=""> <p></p> <script> var baseUrl = 'http://api.douban.com/v2/movie/' function http({ url, success }){ $.ajax({ url:baseUrl+url, dataType:"jsonp", success:res=>{ success(res) } }) } $("button").click(function(){ var value = $("input").val(); http({ url:`search?q=${value}&count=1`, success:res=>{ var title = res.title; var imgUrl = res.subjects[0].images.small; var id = res.subjects[0].id; $("h1").html(title); $("img").attr("src",imgUrl) http({ url:id, success:res=>{ var summary = res.summary; $("p").html(summary) } }) } }) }) </script> </body> ``` ## **promise解决回调地狱** ``` <body> <input /> <button>搜索一下</button> <h1></h1> <img src="" alt=""> <p></p> <script> var baseUrl = 'http://api.douban.com/v2/movie/' function http(url) { /*暂停,凝固*/ return new Promise((resolve, reject) => { $.ajax({ url:baseUrl+url, dataType: "jsonp", success: res => { resolve(res) }, error: err => { reject(err); } }) }) } $("button").click(function () { var value = $("input").val(); http(`search?q=${value}&count=1`).then(res => { var id = res.subjects[0].id; $("img").attr("src", res.subjects[0].images.small); $("h1").html(res.title); return http(id) }).then(res => { var summary = res.summary; $("p").html(summary) }) }) </script> </body> ``` ## 用generator解决回调地狱 `<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script>` ``` <body> <input /> <button>搜索一下</button> <h1></h1> <img src="" alt=""> <p></p> <script> var baseUrl = 'http://api.douban.com/v2/movie/'; function *getData(){ var id = yield $.ajax({ url:baseUrl+"search?q=你的名字&count=1", dataType:"jsonp" }) yield $.ajax({ url:baseUrl+id, dataType:"jsonp" }) } var gen = getData(); var movie = gen.next(); movie.value.then(res=>{ var id = res.subjects[0].id; var detail = gen.next(id); detail.value.then(res=>{ console.log(res) }) }) </script> </body> ``` ## 用async解决回调地狱 `<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script>` ``` <body> <script> var baseUrl = 'http://api.douban.com/v2/movie/'; async function http({ url }){ return $.ajax({ url:baseUrl+url, dataType:"jsonp" }) } async function getData(){ var data = await http({url:"search?q=你的名字&count=1"}); var id = data.subjects[0].id; var detail = await http({url:id}); console.log(data); console.log(detail); } getData() </script> </body> ```