You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
letp1=newPromise((resolve,reject)=>{setTimeout(()=>{resolve('success')},1000)})letp2=newPromise((resolve,reject)=>{setTimeout(()=>{reject('failed')},500)})Promise.race([p1,p2]).then((result)=>{console.log(result)}).catch((error)=>{console.log(error)// out 'failed'})
The text was updated successfully, but these errors were encountered:
songning0605
changed the title
Promise.all 和 Promise.race 的区别Promise.all 和 Promise.race 的区别
Aug 31, 2020
songning0605
changed the title
Promise.all 和 Promise.race 的区别
Promise.all 和 Promise.race 的区别
Aug 31, 2020
参考:理解和使用
Promise.all
和Promise.race
Pomise.all
Promise.all()
方法用于将多个Promise
实例,包装成一个新的Promise
实例。Promise.all()
成功和失败的返回值是不同的,成功的时候返回的是一个结果数组,而失败的时候则返回最先被reject的值。Promise.all()
的结果是成功时,返回结果的数组里边的数据顺序和Promise.all()
接收到的Promise
顺序是一致的。Promise.race
的使用顾名思义,
Promse.race
就是赛跑的意思,意思就是说,Promise.race([p1, p2, p3])
里面哪个结果获得的快,就返回那个结果,不管结果本身是成功状态还是失败状态,其他Promise
的代码还会执行,只是不会返回。The text was updated successfully, but these errors were encountered: