第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

奇怪的 Unhandled promise rejection

奇怪的 Unhandled promise rejection

狐的傳說(shuō) 2019-05-12 15:35:35
如下代碼,一直輸出Unhandledpromiserejection(rejectionid:1):error1,怎么避免?主要功能為getVal第一次調(diào)用預(yù)加載,第二次調(diào)用時(shí)沒(méi)返回就等,返回了就直接取值constrejects=[0,1]leti=0functiongetSummary(id){returnnewPromise((resolve,reject)=>{console.log('rr',id,i)if(rejects.includes(i)){setTimeout(()=>reject('error1'),200)}elseif(id===1){setTimeout(()=>resolve(id),200)}else{setTimeout(()=>resolve(id),500)}i++})}classTest{test(){this.getVal()setTimeout(async()=>{try{constval=awaitthis.getVal()console.log('get1',val)}catch(ex){console.log('error')}console.log('get2',awaitthis.getVal())},1000)}asyncgetVal(){try{if(!this.summary){this.summary=getSummary(1).catch(()=>getSummary(2)).then((data)=>{this.summary=datareturndata})}elseif(this.summary.then){returnawaitthis.summary}}catch(ex){this.summary=null}returnthis.summary}}newTest().test()已解決見(jiàn)3樓如1樓所說(shuō),第一個(gè)getVal異常被上拋了,直接trycatch也不行,只能Await才行。只好去看await改成3樓的了
查看完整描述

2 回答

?
米琪卡哇伊

TA貢獻(xiàn)1998條經(jīng)驗(yàn) 獲得超6個(gè)贊

是不是這個(gè)意思
asyncgetVal(){
try{
if(!this.summary){
//加上await
this.summary=awaitgetSummary(1).catch(()=>getSummary(2))
//后面的不要了
//.then((data)=>{
//this.summary=data;
//returndata;
//})
}elseif(this.summary.then){
returnawaitthis.summary
}
}catch(ex){
this.summary=null
}
returnthis.summary
}
                            
查看完整回答
1 反對(duì) 回復(fù) 2019-05-12
?
森林海

TA貢獻(xiàn)2011條經(jīng)驗(yàn) 獲得超2個(gè)贊

當(dāng)Promise的狀態(tài)變?yōu)閞ejection時(shí)沒(méi)有正確處理,讓其一直冒泡(propagation),直至被進(jìn)程捕獲。這個(gè)Promise就被稱為unhandledpromiserejection。
//方式一.then(undefined,()=>{})
newPromise((resolve,reject)=>{
//...
reject('timeout');
}).then(undefined,(error)=>{
console.error(error);
});
//方式二.catch(()=>{})
newPromise((resolve,reject)=>{
//...
reject('timeout')
}).catch((error)=>{
console.error(error);
})
關(guān)鍵點(diǎn)就是不要讓異常上拋。
                            
查看完整回答
反對(duì) 回復(fù) 2019-05-12
  • 2 回答
  • 0 關(guān)注
  • 5718 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)