title: Promise使用
categories: Javascript
tag:
- promise
date: 2021-11-29 21:16:34
Promise 使用详解
我们调用一个函数,这个函数中发送网络请求(我们可以用定时器来模拟);
- 如果发送网络请求成功了,那么告知调用者发送成功,并且将相关数据返回过去;
- 如果发送网络请求失败了,那么告知调用者发送失败,并且告知错误信息;
在以前,我们使用的是传入回调函数。
在上面的解决方案中,我们确确实实可以解决请求函数得到结果之后,获取到对应的回调,但是它存在两个主要的
问题:
- 第一,我们需要自己来设计回调函数、回调函数的名称、回调函数的使用等;
- 第二,对于不同的人、不同的框架设计出来的方案是不同的,那么我们必须耐心去看别人的源码或者文档,以便可以理解它这个函数到底怎么用;
什么是 Promise?
我们来看一下 Promise 的 API 是怎么样的:
- Promise 是一个类,可以翻译成 承诺、许诺 、期约;
- 当我们需要给予调用者一个承诺:待会儿我会给你回调数据时,就可以创建一个 Promise 的对象;
- 在通过 new 创建 Promise 对象时,我们需要传入一个回调函数,我们称之为 executor
- 这个回调函数会被立即执行,并且给传入另外两个回调函数 resolve、reject;
- 当我们调用 resolve 回调函数时,会执行 Promise 对象的 then 方法传入的回调函数;
- 当我们调用 reject 回调函数时,会执行 Promise 对象的 catch 方法传入的回调函数;
//Promise是一个构造函数
// > resolve: 成功时调用的回调函数
// > reject: 失败时调用的回调函数
// then方法传入的函数会在Promise执行resolve函数的时候回调
// catch方法传入的函数会在Promise执行reject函数的时候回调
const promise = new Promise((resolve, reject) => {
console.log('promise执行了')
resolve()
})
.then()
.catch()
重构导语中的网络请求
function requestData(url, success, failure) {
return new Promise((resolve, reject) => {
setTimeout(() => {
//拿到请求的结果
if (url == 'why') {
//成功
let names = ['abc', 'cba']
resolve(names)
} else {
//失败
let errMessage = '请求失败了'
reject(errMessage)
}
}, 2000)
})
}
const promise = requestData('why')
//then 可以传递两个回调函数。也可以分开传
// 第一个回调方法传入的函数会在Promise执行resolve函数的时候回调
// 第二个回调传入的函数会在Promise执行reject函数的时候回调
promise.then(
(res) => {
console.log(res)
},
(err) => {
console.log(err)
}
)
Executor
Executor 是在创建 Promise 时需要传入的一个回调函数,这个回调函数会被立即执行,并且传入两个参数:
通常我们会在 Executor 中确定我们的 Promise 状态:
- 通过 resolve,可以兑现(fulfilled)Promise 的状态,我们也可以称之为已决议(resolved);
- 通过 reject,可以拒绝(reject)Promise 的状态;
这里需要注意:一旦状态被确定下来,Promise 的状态会被 锁死,该 Promise 的状态是不可更改的 (也就是 resolve()和 reject()只会生效一个)
- 在我们调用 resolve 的时候,如果 resolve 传入的值本身不是一个 Promise,那么会将该 Promise 的状态变成兑现(fulfilled);
- 在之后我们去调用 reject 时,已经不会有任何的响应了(并不是这行代码不会执行,而是无法改变 Promise 状态);
new Promise((resolve, reject) => {
console.log('-------')
resolve() //处于fulfilled状态(已经敲定)
// reject() //处于reject状态(已拒绝状态)
}).then(
(res) => {
console.log(res)
},
(err) => {
console.log(err)
}
)
resolve 参数
resolve 不同值的区别
- 情况一:如果 resolve 传入一个普通的值或者对象,那么这个值会作为 then 回调的参数;
- 情况二:如果 resolve 中传入的是另外一个 Promise,那么这个新 Promise 会决定原 Promise 的状态:
- 情况三:如果 resolve 中传入的是一个对象,并且这个对象有实现 then 方法,那么会执行该 then 方法,并且根据 then 方法的结果来决定 Promise 的状态:
Promise 实例方法
then 方法
then 方法是 Promise 对象上的一个方法:它其实是放在 Promise 的原型上的 Promise.prototype.then
then 方法接受两个参数:
- fulfilled 的回调函数:当状态变成 fulfilled 时会回调的函数;
- reject 的回调函数:当状态变成 reject 时会回调的函数;
一个 Promise 的 then 方法是可以被多次调用的:
- 每次调用我们都可以传入对应的 fulfilled 回调;
- 当 Promise 的状态变成 fulfilled 的时候,这些回调函数都会被执行;
一个 Promise 的 then 方法是可以被多次调用的:案例
then 的返回值
then 方法本身是有返回值的,它的返回值是一个 Promise,所以我们可以进行如下的链式调用:
- 但是 then 方法返回的 Promise 到底处于什么样的状态呢?
Promise 有三种状态,那么这个 Promise 处于什么状态呢?
- 当 then 方法中的回调函数本身在执行的时候,那么它处于 pending 状态;
- 当 then 方法中的回调函数返回一个结果时,那么它处于 fulfilled 状态,并且会将结果作为 resolve 的参数;
- 情况一:返回一个普通的值;
- 情况二:返回一个 Promise;
- 情况三:返回一个 thenable 值;
- 当 then 方法抛出一个异常时,那么它处于 reject 状态;
- 如果返回的是普通值。内部会产生 promise,并且以返回值 resolve 出来
- 如果返回的是 Promise 对象
- 如果返回的是对象,并且有 then 方法
catch 方法
catch 方法也是 Promise 对象上的一个方法:它也是放在 Promise 的原型上的 Promise.prototype.catch
一个 Promise 的 catch 方法是可以被多次调用的:
- 每次调用我们都可以传入对应的 reject 回调;
- 当 Promise 的状态变成 reject 的时候,这些回调函数都会被执行;
const promise = new Promise((resolve, reject) => {
throw new Error('rejected Status')
})
//也可以捕获throw new Error
promise.catch((err) => {
console.log(err)
})
注意的是
但是当 then 返回的是 promise
catch 返回值
事实上 catch 方法也是会返回一个 Promise 对象的,所以 catch 方法后面我们可以继续调用 then 方法或者 catch 方法:
下面的代码,后续是 catch 中的 err2 打印,还是 then 中的 res 打印呢?
答案是 res 打印,这是因为 catch 传入的回调在执行完后,默认状态依然会是 fulfilled 的;
那么如果我们希望后续继续执行 catch,那么需要抛出一个异常;
finally
finally 是在 ES9(ES2018)中新增的一个特性:表示无论 Promise 对象无论变成 fulfilled 还是 reject 状态,最终都会被执行的代码。
finally 方法是不接收参数的,因为无论前面是 fulfilled 状态,还是 reject 状态,它都会执行。
const promise = new Promise((resolve, reject) => {
reject('111')
})
promise
.then((res) => {
console.log(res)
})
.catch((err) => {
console.log(err)
})
//必会执行finally
.finally(() => {
console.log('finally')
})
Promise 类方法
resolve 方法
前面我们学习的 then、catch、finally 方法都属于 Promise 的实例方法,都是存放在 Promise 的 prototype 上的。
有时候我们已经有一个现成的内容了,希望将其转成 Promise 来使用,这个时候我们可以使用 Promise.resolve 方法来完成。
- Promise.resolve 的用法相当于 new Promise,并且执行 resolve 操作:
Promise.resolve('why')
// 相当于
new Promise((resolve, reject) => resolve('why'))
resolve 参数的形态:
- 情况一:参数是一个普通的值或者对象
- 情况二:参数本身是 Promise
- 情况三:参数是一个 thenable
reject 方法
reject 方法类似于 resolve 方法,只是会将 Promise 对象的状态设置为 reject 状态。
Promise.reject 的用法相当于 new Promise,只是会调用 reject:
Promise.reject('why')
// 相当于
new Promise((resolve, reject) => reject('why'))
Promise.reject 传入的参数无论是什么形态,都会直接作为 reject 状态的参数传递到 catch 的。
all 方法
另外一个类方法是 Promise.all:
- 它的作用是将多个 Promise 包裹在一起形成一个新的 Promise;
- 新的 Promise 状态由包裹的所有 Promise 共同决定:
- 当所有的 Promise 状态变成 fulfilled 状态时,新的 Promise 状态为 fulfilled,并且会将所有 Promise 的返回值 组成一个数组;
- 当有一个 Promise 状态为 reject 时,新的 Promise 状态为 reject,并且会将第一个 reject 的返回值作为参数;
const promise1 = new Promise((resolve, reject) => {
setTimeout(() => {
resolve(111)
}, 4000)
})
const promise2 = new Promise((resolve, reject) => {
setTimeout(() => {
resolve(222)
}, 2000)
})
const promise3 = new Promise((resolve, reject) => {
setTimeout(() => {
resolve(333)
}, 3000)
})
Promise.all([promise1, promise2, promise3]).then((res) => {
console.log(res) //[ 111, 222, 333 ]
})
当有一个 Promise 状态为 reject 时,新的 Promise 状态为 reject,并且会将第一个 reject 的返回值作为参数;
allSettled 方法
all 方法有一个缺陷:当有其中一个 Promise 变成 reject 状态时,新 Promise 就会立即变成对应的 reject 状态。
- 那么对于 resolved 的,以及依然处于 pending 状态的 Promise,我们是获取不到对应的结果的;
在 ES11(ES2020)中,添加了新的 API Promise.allSettled:
- 该方法会在所有的 Promise 都有结果(settled),无论是 fulfilled,还是 reject 时,才会有最终的状态;
- 并且这个 Promise 的结果一定是 fulfilled 的;
race 方法
如果有一个 Promise 有了结果,我们就希望决定最终新 Promise 的状态,那么可以使用 race 方法:
- race 是竞技、竞赛的意思,表示多个 Promise 相互竞争,谁先有结果,那么就使用谁的结果;
如果最先的结果是拒绝,也不会继续执行了
const promise1 = new Promise((resolve, reject) => {
setTimeout(() => {
resolve(111)
}, 1000)
})
const promise2 = new Promise((resolve, reject) => {
setTimeout(() => {
reject('我拒绝')
}, 2000)
})
const promise3 = new Promise((resolve, reject) => {
setTimeout(() => {
resolve(333)
}, 3000)
})
Promise.race([promise1, promise2, promise3])
.then((res) => {
console.log(res)
})
.catch((err) => {
console.log(err)
})
any 方法
如果我们非要想等一个 resolve 的结果呢。n
any 方法是 ES12 中新增的方法,和 race 方法是类似的:
any 方法会等到一个 fulfilled 状态,才会决定新 Promise 的状态;
如果所有的 Promise 都是 reject 的,那么也会等到所有的 Promise 都变成 rejected 状态;
const promise1 = new Promise((resolve, reject) => {
setTimeout(() => {
resolve(111)
}, 4000)
})
const promise2 = new Promise((resolve, reject) => {
setTimeout(() => {
reject('我拒绝')
}, 2000)
})
const promise3 = new Promise((resolve, reject) => {
setTimeout(() => {
resolve(333)
}, 3000)
})
Promise.any([promise1, promise2, promise3])
.then((res) => {
console.log(res)
})
.catch((err) => {
console.log(err)
})
//返回结果是3
如果所有的都是 reject,运行结果为:
AggregateError: All promises were rejected
手写 Promise
Promise 状态设计
首先,关于 Promise 状态的设计
const PROMISE_STATUS_FULFILLED = 'fulfilled'
const PROMISE_STATUS_PENDING = 'pending'
const PROMISE_STATUS_REJECTED = 'rejected'
class HyPromise {
constructor(executor) {
this.status = PROMISE_STATUS_PENDING
const resolve = () => {
if (this.status == PROMISE_STATUS_PENDING) {
this.status = PROMISE_STATUS_FULFILLED
console.log('resolve被调用')
}
}
const reject = () => {
if (this.status == PROMISE_STATUS_PENDING) {
this.status = PROMISE_STATUS_REJECTED
console.log('reject被调用')
}
}
executor(resolve, reject)
}
}
const promise = new HyPromise((resolve, reject) => {
resolve()
reject()
})
传递参数
然后,我们需要给 resolve,reject 传递参数
所以我们需要有 value 和 reason 参数
then/catch 实现
我们在 then 里面添加了onFulfilled, onRejected。把外界调用 then 传入的函数作为onFulfilled, onRejected
但是根据执行顺序,这个 then 中的onFulfilled, onRejected还没有获取值,我们使用一个 queueMicrotask 把调用onFulfilled, onRejected函数变为宏任务。
then 多次调用
以上代码写好了之后,我们并不能多次调用 then .会覆盖掉
我们就需要设置两个数组分别保存成功的回调和失败的回调
此时的运行结果
就可以实现多次调用 then 了
如果我们有一个定时器。过了一秒再调用。可是我们写的 promise 不能调用 then
那么,我们可以修改 then 方法
这时候,会发现。会同时执行 resolve 和 reject,那么我们就应该,修改如下代码
then 链式调用
我们知道 then 的返回值是一个新的 promise。那么也可以链式调用 then。
我们知道不管 then 是 resolve 还是 reject。都会调用 resolve
那么我们 then 方法就可以修改为
最后我们的 HyPromise 代码
class HyPromise {
constructor(executor) {
this.status = PROMISE_STATUS_PENDING
this.value = undefined
this.reason = undefined
this.onFulfilledFns = []
this.onRejectedFns = []
const resolve = (value) => {
if (this.status === PROMISE_STATUS_PENDING) {
queueMicrotask(() => {
if (this.status != PROMISE_STATUS_PENDING) return
this.status = PROMISE_STATUS_FULFILLED
this.value = value
this.onFulfilledFns.forEach((fn) => {
fn(this.value)
})
})
}
}
const reject = (reason) => {
if (this.status === PROMISE_STATUS_PENDING) {
queueMicrotask(() => {
if (this.status != PROMISE_STATUS_PENDING) return
this.status = PROMISE_STATUS_REJECTED
this.reason = reason
this.onRejectedFns.forEach((fn) => {
fn(this.reason)
})
})
}
}
try {
executor(resolve, reject)
} catch (err) {
reject(err)
}
}
then(onFulfilled, onRejected) {
return new HyPromise((resolve, reject) => {
if (this.status === PROMISE_STATUS_FULFILLED && onFulfilled) {
try {
const value = onFulfilled(this.value)
resolve(value)
} catch (err) {
reject(err)
}
}
if (this.status === PROMISE_STATUS_REJECTED && onRejected) {
try {
const reason = onRejected(this.reason)
resolve(reason)
} catch (err) {
reject(err)
}
}
if (this.status === PROMISE_STATUS_PENDING) {
this.onFulfilledFns.push(() => {
try {
const value = onFulfilled(this.value)
resolve(value)
} catch (err) {
reject(err)
}
})
this.onRejectedFns.push(() => {
try {
const reason = onRejected(this.reason)
resolve(reason)
} catch (err) {
reject(err)
}
})
}
})
}
}
then 链式调用优化
我们可以看到有很多的 try catch,那么我们可以封装一个工具函数
function execFunctionWithCatchError(execFn, value, resolve, reject) {
try {
const result = execFn(value)
resolve(result)
} catch (err) {
reject(err)
}
}
那么我们就可以优化为以下代码
const PROMISE_STATUS_FULFILLED = 'fulfilled'
const PROMISE_STATUS_PENDING = 'pending'
const PROMISE_STATUS_REJECTED = 'rejected'
//工具函数
function execFunctionWithCatchError(execFn, value, resolve, reject) {
try {
const result = execFn(value)
resolve(result)
} catch (err) {
reject(err)
}
}
class HyPromise {
constructor(executor) {
this.status = PROMISE_STATUS_PENDING
this.value = undefined
this.reason = undefined
this.onFulfilledFns = []
this.onRejectedFns = []
const resolve = (value) => {
if (this.status === PROMISE_STATUS_PENDING) {
queueMicrotask(() => {
if (this.status != PROMISE_STATUS_PENDING) return
this.status = PROMISE_STATUS_FULFILLED
this.value = value
this.onFulfilledFns.forEach((fn) => {
fn(this.value)
})
})
}
}
const reject = (reason) => {
if (this.status === PROMISE_STATUS_PENDING) {
queueMicrotask(() => {
if (this.status != PROMISE_STATUS_PENDING) return
this.status = PROMISE_STATUS_REJECTED
this.reason = reason
this.onRejectedFns.forEach((fn) => {
fn(this.reason)
})
})
}
}
try {
executor(resolve, reject)
} catch (err) {
reject(err)
}
}
then(onFulfilled, onRejected) {
return new HyPromise((resolve, reject) => {
if (this.status === PROMISE_STATUS_FULFILLED && onFulfilled) {
// try {
// const value = onFulfilled(this.value)
// resolve(value)
// } catch (err) {
// reject(err)
// }
execFunctionWithCatchError(onFulfilled, this.value, resolve, reject)
}
if (this.status === PROMISE_STATUS_REJECTED && onRejected) {
// try {
// const reason = onRejected(this.reason)
// resolve(reason)
// } catch (err) {
// reject(err)
// }
execFunctionWithCatchError(onRejected, this.reason, resolve, reject)
}
if (this.status === PROMISE_STATUS_PENDING) {
this.onFulfilledFns.push(() => {
// try {
// const value = onFulfilled(onFulfilled)
// resolve(value)
// } catch (err) {
// reject(err)
// }
execFunctionWithCatchError(onFulfilled, this.value, resolve, reject)
})
this.onRejectedFns.push(() => {
// try {
// const reason = onRejected(this.reason)
// resolve(reason)
// } catch (err) {
// reject(err)
// }
execFunctionWithCatchError(onRejected, this.reason, resolve, reject)
})
}
})
}
}
//测试代码================================================================
const promise = new HyPromise((resolve, reject) => {
reject('222222222')
})
promise
.then(
(res) => {
console.log('res1:', res)
return 11111
},
(err) => {
console.log('err1:', err)
return 'nnnnnn'
}
)
.then(
(res) => {
console.log('res2:', res)
},
(err) => {
console.log(err)
}
)
// promise.then(
// (res) => {
// console.log('res2:', res)
// },
// (err) => {
// console.log('err2:', err)
// }
// )
// setTimeout(() => {
// promise.then(
// (res) => {
// console.log('res3:', res)
// },
// (err) => {
// console.log('err3:', err)
// }
// )
// }, 1000)
实现 catch 方法
本来我们应该是
.then(res(),error())
当我们调用 catch 的时候,希望链式使用。
那么最走 then 方法和 catch 方法为如下代码
then(onFulfilled, onRejected) {
onRejected =
onRejected ||
((err) => {
throw err
})
return new HyPromise((resolve, reject) => {
if (this.status === PROMISE_STATUS_FULFILLED && onFulfilled) {
execFunctionWithCatchError(onFulfilled, this.value, resolve, reject)
}
if (this.status === PROMISE_STATUS_REJECTED && onRejected) {
execFunctionWithCatchError(onRejected, this.reason, resolve, reject)
}
if (this.status === PROMISE_STATUS_PENDING) {
if (onFulfilled) {
this.onFulfilledFns.push(() => {
execFunctionWithCatchError(onFulfilled, this.value, resolve, reject)
})
}
if (onRejected) {
this.onRejectedFns.push(() => {
execFunctionWithCatchError(onRejected, this.reason, resolve, reject)
})
}
}
})
}
catch(onRejected) {
this.then(undefined, onRejected)
}
finally 方法
我们在第一个 promise 调用 resolve(1111),运行结果如下
但是当我们调用 catch 的时候,就不能正确执行 finally 了。如果我们在第一个 promise 调用 reject(就可以正常执行)
这是由于在 catch 中,我们把 resolve 设置为了 undefned。就不会向下执行了。
为了不断层,我们改一下 then 方法,要返回 value
resolve,reject 类方法
all 类方法
allSettled 类方法
运行结果
race 方法
any 方法
const p1 = new Promise((resolve, reject) => {
setTimeout(() => {
// resolve(111)
reject(111)
}, 4000)
})
const p2 = new Promise((resolve, reject) => {
setTimeout(() => {
// resolve(222)
reject(222)
}, 3000)
})
const p3 = new Promise((resolve, reject) => {
setTimeout(() => {
reject(3333)
}, 2000)
})
HyPromise.any([p1, p2, p3])
.then((res) => {
console.log('res:', res)
})
//可以通过errors获得具体错误情况
.catch((err) => {
console.log('err:', err.errors)
})
以上就完成了。
那么最终 Promise 的代码为
const PROMISE_STATUS_FULFILLED = 'fulfilled'
const PROMISE_STATUS_PENDING = 'pending'
const PROMISE_STATUS_REJECTED = 'rejected'
//工具函数
function execFunctionWithCatchError(execFn, value, resolve, reject) {
try {
const result = execFn(value)
resolve(result)
} catch (err) {
reject(err)
}
}
class HyPromise {
constructor(executor) {
this.status = PROMISE_STATUS_PENDING
this.value = undefined
this.reason = undefined
this.onFulfilledFns = []
this.onRejectedFns = []
const resolve = (value) => {
if (this.status === PROMISE_STATUS_PENDING) {
queueMicrotask(() => {
if (this.status != PROMISE_STATUS_PENDING) return
this.status = PROMISE_STATUS_FULFILLED
this.value = value
this.onFulfilledFns.forEach((fn) => {
fn(this.value)
})
})
}
}
const reject = (reason) => {
if (this.status === PROMISE_STATUS_PENDING) {
queueMicrotask(() => {
if (this.status != PROMISE_STATUS_PENDING) return
this.status = PROMISE_STATUS_REJECTED
this.reason = reason
this.onRejectedFns.forEach((fn) => {
fn(this.reason)
})
})
}
}
try {
executor(resolve, reject)
} catch (err) {
reject(err)
}
}
then(onFulfilled, onRejected) {
const defaultOnRejected = (err) => {
throw err
}
onRejected = onRejected || defaultOnRejected
const defaultOnFulfilled = (value) => {
return value
}
onFulfilled = onFulfilled || defaultOnFulfilled
return new HyPromise((resolve, reject) => {
if (this.status === PROMISE_STATUS_FULFILLED && onFulfilled) {
execFunctionWithCatchError(onFulfilled, this.value, resolve, reject)
}
if (this.status === PROMISE_STATUS_REJECTED && onRejected) {
execFunctionWithCatchError(onRejected, this.reason, resolve, reject)
}
if (this.status === PROMISE_STATUS_PENDING) {
if (onFulfilled) {
this.onFulfilledFns.push(() => {
execFunctionWithCatchError(onFulfilled, this.value, resolve, reject)
})
}
if (onRejected) {
this.onRejectedFns.push(() => {
execFunctionWithCatchError(onRejected, this.reason, resolve, reject)
})
}
}
})
}
catch(onRejected) {
return this.then(undefined, onRejected)
}
finally(onFinally) {
this.then(
() => {
onFinally()
},
() => {
onFinally()
}
)
}
static resolve(value) {
return new HyPromise((resolve) => resolve(value))
}
static reject(reason) {
return new HyPromise((resolve, reject) => reject(reason))
}
static all(promises) {
return new HyPromise((resolve, reject) => {
const values = []
promises.forEach((promise) => {
promise.then(
(res) => {
values.push(res)
if (values.length == promises.length) {
resolve(values)
}
},
(err) => {
reject(err)
}
)
})
})
}
static allSettled(promises) {
return new HyPromise((resolve) => {
const results = []
promises.forEach((promise) => {
promise.then(
(res) => {
results.push({ satus: PROMISE_STATUS_FULFILLED, value: res })
if (results.length == promises.length) {
resolve(results)
}
},
(err) => {
results.push({ satus: PROMISE_STATUS_REJECTED, value: err })
if (results.length == promises.length) {
resolve(results)
}
}
)
})
})
}
static race(promises) {
return new HyPromise((resolve, reject) => {
promises.forEach((promise) => {
promise.then(
(res) => {
resolve(res)
},
(err) => {
reject(err)
}
)
})
})
}
static any(promises) {
const reasons = []
return new HyPromise((resolve, reject) => {
promises.forEach((promise) => {
promise.then(
(res) => {
resolve(res)
},
(err) => {
reasons.push(err)
if (reasons.length == promises.length) {
reject(new AggregateError(reasons))
}
}
)
})
})
}
}
//测试代码================================================================
const promise = new HyPromise((resolve, reject) => {
resolve(11111)
// reject(22222)
})
const p1 = new Promise((resolve, reject) => {
setTimeout(() => {
// resolve(111)
reject(111)
}, 4000)
})
const p2 = new Promise((resolve, reject) => {
setTimeout(() => {
// resolve(222)
reject(222)
}, 3000)
})
const p3 = new Promise((resolve, reject) => {
setTimeout(() => {
reject(3333)
}, 2000)
})
HyPromise.any([p1, p2, p3])
.then((res) => {
console.log('res:', res)
})
//可以通过errors获得具体错误情况
.catch((err) => {
console.log('err:', err.errors)
})
// HyPromise.race([p1, p2, p3])
// .then((res) => {
// console.log('res:', res)
// })
// .catch((err) => {
// console.log('err:', err)
// })
// HyPromise.allSettled([p1, p2, p3])
// .then((res) => {
// console.log(res)
// })
// .catch((err) => {
// console.log(err)
// })
// promise
// .then((res) => {
// console.log('res1:', res)
// return 'aaaa'
// })
// .catch((err) => {
// console.log('err:', err)
// })
// .finally(() => {
// console.log('finally')
// })
// HyPromise.resolve('hello').then((res) => {
// console.log(res)
// })
// HyPromise.reject('err').catch((err) => {
// console.log(err)
// })
// promise
// .then(
// (res) => {
// console.log('res1:', res)
// return 11111
// },
// (err) => {
// console.log('err1:', err)
// return 'nnnnnn'
// }
// )
// .then(
// (res) => {
// console.log('res2:', res)
// },
// (err) => {
// console.log(err)
// }
// )
// promise.then(
// (res) => {
// console.log('res2:', res)
// },
// (err) => {
// console.log('err2:', err)
// }
// )
// setTimeout(() => {
// promise.then(
// (res) => {
// console.log('res3:', res)
// },
// (err) => {
// console.log('err3:', err)
// }
// )
// }, 1000)