Promise静态方法 race
race 使用方法:
参数为数组类型,返回的是第一个兑现或者第一个失败的值
并且参数不为数组会报错,必须传入一个promise可迭代的对象作为输入,返回一个promise对象并且会随传入数组中第一个promise对象的敲定而敲定,第一个状态改变
实现的核心步骤:判断是否为数组 =》是则等待第一个敲定
<script>static race(value){return new Promise(resolve,reject)=>{// 判断是否为数组 if(!Array.isArray(value)){reject(new TypeError(`传入值不是数组`))}// 等待第一个敲定value.forEach((item)=>{const curPromise = Promise.resolve(item).then(res=>{resolve(res)},err=>{reject(err)})})}}</script>