当打开一个页面需要大量初始信息时,需要调用几个接口。如果采用异步嵌套的方式进行排查,会严重延长页面的打开时间。页面打开时间是所有界面打开时间的总和。所以需要并行请求所有接口,然后只需要等待最慢的接口返回即可。那么页面打开时间就是最慢的接口返回数据的时间。方案一:先同时请求所有接口,然后启动await。constuserInfoFc=this.getUser();constauthInfoFc=this.getAuth();constorgInfoFc=this.getOrgTree();//await命令后跟一个Promise对象constuserInfo=awaituserInfoFc;constauthInfo=awaitauthInfoFc;constorgInfo=awaitorgInfoFc;Scheme2let[userInfo,authInfo]=awaitPromise.all([this.getUser(),this.getAuth()]);PS:Promise.all如果在开发过程中其中一个失败,如何制作程序不要离开catch?let[userInfo,authInfo]=awaitPromise.all([this.getUser(),this.getAuth()].map(p=>p.catch(e=>e)));方案3constpromise1=Promise。解决(3);constpromise2=newPromise((resolve,reject)=>setTimeout(reject,100,'foo'));constpromises=[promise1,promise2];承诺。allSettled(promises).then((results)=>results.forEach((result)=>console.log(result)));//预期输出://"fulfilled"//"rejected"MDN官方文档
