1 异步调用
- 异步效果分析
① 定时任务
② Ajax
③ 事件函数
- 多次异步调用的依赖分析
① 多次异步调用的结果顺序不确定
② 异步调用结果如果存在依赖需要嵌套
2 Promise用法
- 主要解决异步深层嵌套的问题 promise 提供了
- 简洁的API 使得异步操作更加容易
3 Promise基本用法
- 实例化Promise对象,构造函数中传递函数,该函数中用于处理异步任务。
- resolve和reject两个参数用于处理成功和失败两种情况,并通过p.then获取处理结果。
| var p = new Promise(function(resolve, reject){ |
| |
| |
| }); |
| p.then(function(ret){ |
| |
| },function(ret){ |
| |
| }); |
| <!DOCTYPE html> |
| <html lang="en"> |
| <head> |
| <meta charset="UTF-8"> |
| <title>Document</title> |
| </head> |
| <body> |
| |
| <script type="text/javascript"> |
| |
| |
| |
| |
| |
| |
| var p = new Promise(function(resolve, reject){ |
| |
| setTimeout(function(){ |
| var flag = false; |
| if(flag) { |
| |
| resolve('hello'); |
| }else{ |
| |
| reject('出错了'); |
| } |
| }, 100); |
| }); |
| p.then(function(data){ |
| console.log(data) |
| },function(info){ |
| console.log(info) |
| }); |
| </script> |
| </body> |
| </html> |
4 基于Promise处理Ajax请求
4.1 处理原生Ajax
4.2 发送多次Ajax请求
| <!DOCTYPE html> |
| <html lang="en"> |
| <head> |
| <meta charset="UTF-8"> |
| <title>Document</title> |
| </head> |
| <body> |
| |
| <script type="text/javascript"> |
| |
| |
| |
| function queryData(url) { |
| var p = new Promise(function(resolve, reject){ |
| var xhr = new XMLHttpRequest(); |
| xhr.onreadystatechange = function(){ |
| if(xhr.readyState != 4) return; |
| if(xhr.readyState == 4 && xhr.status == 200) { |
| |
| resolve(xhr.responseText); |
| }else{ |
| |
| reject('服务器错误'); |
| } |
| }; |
| xhr.open('get', url); |
| xhr.send(null); |
| }); |
| return p; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| queryData('http://localhost:3000/data') |
| .then(function(data){ |
| console.log(data) |
| return queryData('http://localhost:3000/data1'); |
| }) |
| .then(function(data){ |
| console.log(data); |
| return queryData('http://localhost:3000/data2'); |
| }) |
| .then(function(data){ |
| console.log(data) |
| }); |
| </script> |
| </body> |
| </html> |
5 then参数中的函数返回值
5.1 返回Promise实例对象
5.2 返回普通值
- 返回的普通值会直接传递给下一个then,通过then参数中函数的参数接收该值
| <!DOCTYPE html> |
| <html lang="en"> |
| <head> |
| <meta charset="UTF-8"> |
| <title>Document</title> |
| </head> |
| <body> |
| |
| <script type="text/javascript"> |
| |
| |
| |
| function queryData(url) { |
| return new Promise(function(resolve, reject){ |
| var xhr = new XMLHttpRequest(); |
| xhr.onreadystatechange = function(){ |
| if(xhr.readyState != 4) return; |
| if(xhr.readyState == 4 && xhr.status == 200) { |
| |
| resolve(xhr.responseText); |
| }else{ |
| |
| reject('服务器错误'); |
| } |
| }; |
| xhr.open('get', url); |
| xhr.send(null); |
| }); |
| } |
| queryData('http://localhost:3000/data') |
| .then(function(data){ |
| return queryData('http://localhost:3000/data1'); |
| }) |
| .then(function(data){ |
| return new Promise(function(resolve, reject){ |
| setTimeout(function(){ |
| resolve(123); |
| },1000) |
| }); |
| }) |
| .then(function(data){ |
| return 'hello'; |
| }) |
| .then(function(data){ |
| console.log(data) |
| }) |
| |
| </script> |
| </body> |
| </html> |
6 Promise常用API
6.1 实例方法
- .then() 得到异步任务正确的结果
- .catch() 获取异常信息
- .finally() 成功与否都会执行(不是正式标准)
| <!DOCTYPE html> |
| <html lang="en"> |
| <head> |
| <meta charset="UTF-8"> |
| <title>Document</title> |
| </head> |
| <body> |
| |
| <script type="text/javascript"> |
| |
| |
| |
| |
| function foo() { |
| return new Promise(function(resolve, reject){ |
| setTimeout(function(){ |
| |
| reject('error'); |
| }, 100); |
| }) |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| foo() |
| .then(function(data){ |
| console.log(data) |
| },function(data){ |
| console.log(data) |
| }) |
| .finally(function(){ |
| console.log('finished') |
| }); |
| </script> |
| </body> |
| </html> |
6.2 对象方法
- Promise.all() 并发处理多个异步任务,所有任务都执行完成才能得到结果。
- Promise.race() 并发处理多个异步任务,只要有一个任务完成都能得到结果。
| <!DOCTYPE html> |
| <html lang="en"> |
| <head> |
| <meta charset="UTF-8"> |
| <title>Document</title> |
| </head> |
| <body> |
| |
| <script type="text/javascript"> |
| |
| |
| |
| |
| function queryData(url) { |
| return new Promise(function(resolve, reject){ |
| var xhr = new XMLHttpRequest(); |
| xhr.onreadystatechange = function(){ |
| if(xhr.readyState != 4) return; |
| if(xhr.readyState == 4 && xhr.status == 200) { |
| |
| resolve(xhr.responseText); |
| }else{ |
| |
| reject('服务器错误'); |
| } |
| }; |
| xhr.open('get', url); |
| xhr.send(null); |
| }); |
| } |
| |
| var p1 = queryData('http://localhost:3000/a1'); |
| var p2 = queryData('http://localhost:3000/a2'); |
| var p3 = queryData('http://localhost:3000/a3'); |
| |
| |
| |
| Promise.race([p1,p2,p3]).then(function(result){ |
| console.log(result) |
| }) |
| </script> |
| </body> |
| </html> |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)