promise
- 作用: 一种新的异步代码封装方案,用来代替 回调函数的
- promise 的三个状态
- => 持续: pending
- => 成功: fulilled
- => 失败: rejected
- promise 只会在持续状态转换为成功
- promise 的基本语法:promise是JS内置的一个构造函数
- new Promise 得到的对象 我们叫做 promise 对象
- promise 对象上 有一些方法
- => then 方法 (会在promise状态成功的时候 执行)
- => catch 方法 (会在promise状态失败的时候 执行)
const p = new Promise(function(resolve, reject) {
const timer = Math.ceil(Math.random() * 3000)
console.log('让A帮忙去买瓶水')
setTimeout(() => {
if(timer > 200) {
console.log('买水成功,用时', timer)
reject()
} else {
console.log('买水成功,用时', timer)
resolve()
}
}, timer)
})
p.then(() => {
console.log('如果我这行打印,说明promise的状态为 成功')
})
p.catch(() => {
console.log('如果我这行打印,说明promise的状态为 失败')
})
promise的链式调用1
function fn() {
const p = new Promise(function(resolve, reject) {
const timer = Math.ceil(Math.random() * 3000)
console.log('让A帮忙去买瓶水')
setTimeout(() => {
if(timer > 1500) {
console.log('买水成功,用时', timer)
reject('买水失败是因为时间超过了1500毫秒')
} else {
console.log('买水成功,用时', timer)
resolve('买水成功是因为时间小于1500毫秒')
}
}, timer)
})
return p
}
const res = fn()
res.then((str) => {
console.log('如果我这行打印,说明promise的状态为 成功', str)
}).catch((str) => {
console.log('如果我这行打印,说明promise的状态为 失败', str)
})
promise的链式调用2
function fn() {
const p = new Promise(function(resolve, reject) {
const timer = Math.ceil(Math.random() * 3000)
console.log('让A帮忙去买瓶水')
setTimeout(() => {
if(timer > 1500) {
reject('超时,所以买水失败')
} else {
resolve('没有超时,买水成功')
}
}, timer)
})
return p
}
const res = fn()
res.then((str) => {
console.log(`因为${str},所以奖励A10个bug`)
return fn()
}).then((str) => {
console.log(`第二次买水成功`)
return fn()
}).then((str) => {
console.log(`第三次买水成功`)
}).catch((str) => {
console.log(`买水失败`)
})
async 与 await
- 是promise 的一种调用方案 (也就是说必须结合着 promise 一起使用)
- 能够将 异步代码,写得像"同步代码一样"
- async: 书写在一个函数的开头,表明当前函数是一个异步代码,内部可以书写await
- await: 具有等待含义,书写在异步函数前,代码运行到这个位置的时候,会有一个等待效果,一直等到这个异步任务结束,并且将异步任务的反馈结果 当一个值返回出来
function fn() {
const p = new Promise(function(resolve, reject) {
const timer = Math.ceil(Math.random() * 3000)
console.log('让A帮忙去买瓶水')
setTimeout(() => {
if(timer > 1500) {
reject('超时,所以买水失败')
} else {
resolve('没有超时,买水成功')
}
}, timer)
})
return p
}
newFn()
async function newFn() {
const r1 = await fn()
console.log('第1次买水:', r1)
const r2 = await fn()
console.log('第2次买水:', r2)
const r3 = await fn()
console.log('第3次买水:', r3)
}
async与await的缺点
- 不能正常的捕获到promise 的失败状态
-
- try...catch
-
- 封装一个永远不会失败的promise
- 也就是说 不管请求状态如何,永远调用resolve让promise的状态一定是成功
- 为了区分这次请求是成功还是失败,我们不再单纯的返回一个字符串了,而是返回一个对象
- 注意:这个对象 我们约定 里边有一个code属性,成功时赋值为1,失败时赋值为0
function fn() {
const p = new Promise(function(resolve, reject) {
const timer = Math.ceil(Math.random() * 3000)
console.log('让A帮忙去买瓶水')
setTimeout(() => {
if(timer > 1500) {
resolve({
code: 0,
msg: '超时,所以买水失败'
})
} else {
resolve({
code: 0,
msg: '没有超时,所以买水成功'
})
}
}, timer)
})
return p
}
newFn()
async function newFn() {
const r1 = await fn()
console.log(r1)
if(r1.code === 0) {
console.log('请求失败的补救措施')
} else {
console.log('请求成功,正常执行代码即可')
}
}
promise的其他方法
function fn() {
const p = new Promise(function(resolve, reject) {
const timer = Math.ceil(Math.random() * 3000)
console.log('让A帮忙去买瓶水')
setTimeout(() => {
if(timer > 1500) {
reject('超时,所以买水失败')
} else {
resolve('没有超时,买水成功')
}
}, timer)
})
return p
}
Promise.race([fn(), fn(), fn()]).then(() => {
console.log('这些参数中,结束最快的那一个状态为成功的时候执行')
}).catch(() => {
console.log('这些参数中,结束最快的那一个状态为失败的时候执行')
})
3.promise的其他方法
Promise.resolve().then(() => {
console.log('强制返回一个状态为成功的promise')
})
Promise.reject().then(() => {
console.log('如果我打印了,说明当前的promise状态为成功')
}).catch(() => {
console.log('强制返回一个状态为失败的promise对象')
})