// 1.1 一级分类
let xhr1 = new XMLHttpRequest()
xhr1.open('get', 'http://123.57.109.30:3999/api/categoryfirst')
xhr1.send()
xhr1.onload = function () {
console.log('一级')
console.log(JSON.parse(xhr1.response))
// 1.2 二级分类
let xhr2 = new XMLHttpRequest()
xhr2.open('get', 'http://123.57.109.30:3999/api/categorySecond?firstId=621')
xhr2.send()
xhr2.onload = function () {
console.log('二级')
console.log(JSON.parse(xhr2.response))
// 1.3 三级分类
let xhr3 = new XMLHttpRequest()
xhr3.open('get', 'http://123.57.109.30:3999/api/categoryThird?secondId=622')
xhr3.send()
xhr3.onload = function () {
console.log('三级')
console.log(JSON.parse(xhr3.response))
}
}
}
// 1.4 浏览器刷新打印顺序会不同
console.log(666)
// (resolve, reject) 是箭头函数的参数
let pro = new Promise((resolve, reject) => {
// 异步代码
setTimeout(function () {
// resolve(1) // 成功 1是实参
reject(2) // 失败
}, 500)
})
// resolve和reject状态二选一的
pro.then(res => {
console.log(res)
}).catch(error => {
// reject失败则调用catch方法
console.log(error)
})
3.1 Promise是什么? 是ES6新增的构造函数 3.2 Promise作用: 解决回调地狱 3.3 Promise应用场景/原理 Promise对象有三种状态:
let p = new Promise((resolve, reject) => {
// 创建xhr对象
let xhr = new XMLHttpRequest()
// 设置请求方式和url
xhr.open('get', 'http://123.57.109.30:3999/api/categoryfirst')
// 发送ajax请求
xhr.send()
// 注册响应事件函数
xhr.onload = function () {
console.log('一级')
let res = JSON.parse(xhr.response)
// 1.1 Promise成功状态
resolve(res)
}
})
p.then(res => {
console.log(res)
})
p1.then(res => {
console.log(res)
})
p2.then(res => {
console.log(res)
})
p3.then(res => {
console.log(res)
})
function fn(url) {
return new Promise((resolve, reject) => {
let xhr = new XMLHttpRequest()
// 2.1 url是形参 调用时写入实参
xhr.open('get', url)
xhr.send()
xhr.onload = function () {
let res = JSON.parse(xhr.response)
// 2.2 Promise成功状态
resolve(res)
}
})
}
let p1 = fn('http://123.57.109.30:3999/api/categoryfirst')
let p2 = fn('http://123.57.109.30:3999/api/categorySecond?firstId=622')
let p3 = fn('http://123.57.109.30:3999/api/categoryThird?secondId=621')
p1.then(res => {
console.log(res)
return p2
})
.then(res => {
// p2的then
console.log(res)
return p3
})
.then(res => {
// p3的then
console.log(res)
})
function fn(url) {
return new Promise((resolve, reject) => {
let xhr = new XMLHttpRequest()
xhr.open('get', url)
xhr.send()
xhr.onloadend = function () {
let res = JSON.parse(xhr.response)
console.log(res)
resolve(res) // 成功状态
}
})
}
let p1 = fn('http://123.57.109.30:3999/api/categoryfirst')
let p2 = fn('http://123.57.109.30:3999/api/categorySecond?firstId=621')
let p3 = fn('http://123.57.109.30:3999/api/categoryThird?secondId=622')
let p = Promise.all([p1, p2, p3])
// 所有Promise要全部成功 才会执行then 只要有一个失败就会执行catch
p.then(res => {
// then的结果为数组
console.log(res)
}).catch(error => {
console.log(error)
}).finally(() => {
// finally 不管成功失败都执行
console.log('我是finally 我完事了')
})
let pp = Promise.race([p1, p2, p3])
pp.then(res => {
console.log(res, 'race方法')
}).catch(error => {
console.log(error)
})
let p = new Promise((resolve, reject) => {
setTimeout(() => {
resolve(1)
}, 300)
})
// Promise实例对象的then方法
p.then(res => {
console.log(res)
})
async function fn() {
// let res = await Promise对象
let res = await p
console.log(res)
}
fn()
// await箭头函数语法
let fn1 = async () => {
let res = await p
console.log(res)
// 2.2 let res = await 后面的代码
// 如果p是Promise对象 就正常执行Promise
Promise((res, rej) => {
// 如果p不是Promise对象 就会把代码包在Promise里面
}).then(res => {
// 后面的代码在then里
})
}
fn1()
// 1. 封装Promise实例对象函数
function fn(url) {
return new Promise((resolve, reject) => {
let xhr = new XMLHttpRequest()
xhr.open('get', url)
xhr.send()
xhr.onloadend = function () {
let res = JSON.parse(xhr.response)
resolve(res)
}
})
}
// 2. 创建Promise实例
let p1 = fn('http://123.57.109.30:3999/api/categoryfirst')
let p2 = fn('http://123.57.109.30:3999/api/categorySecond?firstId=621')
let p3 = fn('http://123.57.109.30:3999/api/categoryThird?secondId=622')
// 3. Promise通过链式调用解决回调地狱
p1.then(res => {
console.log(res)
return p2
}).then(res => {
console.log(res)
return p3
}).then(res => {
console.log(res)
})
// 01. 使用async/await解决回调地狱
// 1.1 await右边的代码就是Promise内部的代码 .then
async function fn1() {
let res1 = await p1
console.log('一级', res1)
let res2 = await p2
console.log('二级', res2)
let res3 = await p3
console.log('三级', res3)
// 1.2 分析: await下面的代码全都是then里的代码
// p1.then(res1 => {
// console.log(res1)
// p2.then(res2 => {
// console.log(res2)
// p3.then(res3 => {
// console.log(res3)
// })
// })
// })
}
fn1()
axios.get('http://123.57.109.30:3999/api/categoryfirst').then(res => {
console.log(res.data)
})
let p = axios.get('http://123.57.109.30:3999/api/categoryfirst')
console.log(p)
p.then(res => {
console.log(res.data)
})
let fn = {
// 2.1 封装一个get函数
get(url) {
// 2.2 创建Promise实例对象
return new Promise((resolve, reject) => {
// 2.3 这里的代码是原生xhr对象
let xhr = new XMLHttpRequest()
xhr.open('get', url)
xhr.send()
xhr.addEventListener('loadend', function () {
let res = JSON.parse(xhr.response)
resolve(res)
})
})
}
}
// 2.4 调用get方法 会返回Promise对象
let p1 = fn.get('http://hmajax.itheima.net/api/news')
console.log(p1)
// 2.5 取出Promise实例对象结果
p1.then(res => {
console.log(res)
})
// 2.6 简写语法
fn.get('http://hmajax.itheima.net/api/news').then(res => {
console.log(res)
})
// 2.7 使用async/await简写
async function fn1() {
let res = await fn.get('http://hmajax.itheima.net/api/news')
console.log(res)
}
fn1()
// 1. 声明async异步函数
async function fn() {
let res1 = await axios.get('http://123.57.109.30:3999/api/categoryfirst')
console.log(res1.data)
// 2. await调用Promise 会取代Promise的then
// 3. 只要是await后面的代码都在then方法里
let res2 = await axios.get('http://123.57.109.30:3999/api/categorySecond?firstId=621')
console.log(res2.data)
let res3 = await axios.get('http://123.57.109.30:3999/api/categoryThird?secondId=622')
console.log(res3.data)
}
fn()
try {
// 这里代码如果遇到错误 就会执行catch
console.log(1)
// 在try里写throw就会执行catch
throw '错误信息'
} catch (error) {
console.log(error)
} finally {
console.log(666)
}
try {
let arr = [10,20,30,40,50]
arr.forEach(i => {
if (i == 30) {
// throw主动抛出错误 就会结束try代码 然后立即执行catch
throw 2
}
console.log(i)
})
} catch (error) {
console.log(error)
}
async function fn() {
try {
// 错误的Promise走catch 但await走不了catch 只能then
// 所以使用try-catch方法 配合捕捉await错误
let res = await axios.get('http://1hmajax.itheima.net/api/news')
console.log(res.data)
} catch (error) {
console.log(error)
}
}
fn()
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有