亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在打字稿中使用promise.allSettled?

如何在打字稿中使用promise.allSettled?

一只名叫tom的貓 2023-08-18 17:27:11
Typescript 構建失敗,因為它似乎不喜歡,Promise.allSetttled即使我已經設置了 ts config coilerOptions"lib": [ "ES2020.Promise" ],似乎 的響應promise.allSettled不包括resultor reason。運行 typescript build 時出現以下錯誤:Property 'reason' does not exist on type 'PromiseSettledResult<IMyPromiseResult>'.和Property 'value' does not exist on type 'PromiseRejectedResult'.我的代碼塊如下所示,正如您所看到的,我正在嘗試訪問reason每個result已解決的承諾。const myPromise = async () : Promise<IMyPromiseResult> {  return new Promise((resolve) => {    resolve("hello world")  })}const data = await Promise.allSettled([  myPromise()]);const response = data.find(res => res.status === 'fulfilled')?.result;if(!response) {  const error = data.find(res => res.status === 'rejected')?.reason;  throw new Error(error);}如何更新 Promise.allSettled 聲明以包含正確的接口?
查看完整描述

5 回答

?
慕桂英546537

TA貢獻1848條經驗 獲得超10個贊

使用類型保護。比內聯類型保護更優雅的解決方案是將它們定義為單獨的函數,并且通過泛型綁定,您也可以獲得已實現的承諾的正確值,并且可以重用以滿足任何過濾需求allSettled。

不需要鑄造(通常應該避免)。

const isRejected = (input: PromiseSettledResult<unknown>): input is PromiseRejectedResult =>?

? input.status === 'rejected'


const isFulfilled = <T>(input: PromiseSettledResult<T>): input is PromiseFulfilledResult<T> =>?

? input.status === 'fulfilled'


const myPromise = async () => Promise.resolve("hello world");


const data = await Promise.allSettled([myPromise()]);


const response = data.find(isFulfilled)?.value

const error = data.find(isRejected)?.reason


查看完整回答
反對 回復 2023-08-18
?
慕森卡

TA貢獻1806條經驗 獲得超8個贊

TypeScript 在檢查類型時不知道類型是否為PromiseFulfilledResult/ PromiseRejectedResult。


唯一的辦法就是鑄造承諾的結果。之所以可以這樣做,是因為您已經驗證了已解決的承諾已實現或已拒絕。


看這個例子:


const myPromise = async (): Promise<string> => {

? return new Promise((resolve) => {

? ? resolve("hello world");

? });

};


const data = await Promise.allSettled([myPromise()]);


const response = (data.find(

? (res) => res.status === "fulfilled"

) as PromiseFulfilledResult<string> | undefined)?.value;


if (!response) {

? const error = (data.find(

? ? (res) => res.status === "rejected"

? ) as PromiseRejectedResult | undefined)?.reason;

? throw new Error(error);

}


查看完整回答
反對 回復 2023-08-18
?
慕村225694

TA貢獻1880條經驗 獲得超4個贊

使用類型保護:


const isFulfilled = <T,>(p:PromiseSettledResult<T>): p is PromiseFulfilledResult<T> => p.status === 'fulfilled';

const isRejected = <T,>(p:PromiseSettledResult<T>): p is PromiseRejectedResult => p.status === 'rejected';


const results = await Promise.allSettled(...);

const fulfilledValues = results.filter(isFulfilled).map(p => p.value);

const rejectedReasons = results.filter(isRejected).map(p => p.reason);


查看完整回答
反對 回復 2023-08-18
?
動漫人物

TA貢獻1815條經驗 獲得超10個贊

這讓ts不生氣。


const rawResponse = await Promise.allSettled(promiseArray);

const response = rawResponse.filter((res) => res.status === 'fulfilled') as PromiseFulfilledResult<any>[];


const result = response[0].value


查看完整回答
反對 回復 2023-08-18
?
長風秋雁

TA貢獻1757條經驗 獲得超7個贊

將回調定義find為類型保護,返回類型謂詞:


type IMyPromiseResult = string


const response = data.find(

  (res): res is PromiseFulfilledResult<string> => res.status === 'fulfilled'

)?.value;


if (!response) {

  const error = data.find(

    (res): res is PromiseRejectedResult => res.status === 'rejected'

  )?.reason;

  throw new Error(error);

}

演示游樂場


查看完整回答
反對 回復 2023-08-18
  • 5 回答
  • 0 關注
  • 237 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號