簡體   English   中英

RxJS Observable:重試失敗的 Promise

[英]RxJS Observable: retry a Promise that fail

我一直在嘗試創建一個解決 3 個承諾的功能,我這個承諾中的任何一個都失敗了,我想重試。 我的想法是:如果第二個承諾失敗,無論如何都應該解決第一個和第三個承諾,可以通知訂閱者。

那是我的代碼:

const endpointsRequest = [
    axios.get("https://restcountries.eu/rest/v2/name/aruba?fullText=true"),
    axios.get("http://localhost:8080"),
    axios.get("https://restcountries.eu/rest/v2/name/argentina?fullText=true")
];

return Observable
    .forkJoin(endpointsRequest)
    .switchMap( promise => Observable.defer(() => promise) )
    .retry(3)
    .subscribe(
        (x) => {
            console.log("=======================================");
            console.log("Result", x.data.length? x.data[0] : x.data);
            console.log("=======================================");
        },
        (err) => {
            console.log("=======================================");
            console.log(`Error: ${err}`)
            console.log("=======================================");
        },
        () => {
            console.log("=======================================");
            console.log("Completed");
            console.log("=======================================");
        }
    );

有可能實現這一目標嗎?

使用此 promises 數組時的響應示例:

承諾數組

const endpointsRequest = [
        axios.get("https://restcountries.eu/rest/v2/name/aruba?fullText=true"),
        axios.get("https://restcountries.eu/rest/v2/name/argentina?fullText=true")
    ];

回應

=======================================
Result { name: 'Aruba',
  topLevelDomain: [ '.aw' ],
  alpha2Code: 'AW',
  alpha3Code: 'ABW',
  callingCodes: [ '297' ],
  capital: 'Oranjestad',
  altSpellings: [ 'AW' ],
  region: 'Americas',
  subregion: 'Caribbean',
  population: 107394,
  latlng: [ 12.5, -69.96666666 ],
  demonym: 'Aruban',
  area: 180,
  gini: null,
  timezones: [ 'UTC-04:00' ],
  borders: [],
  nativeName: 'Aruba',
  numericCode: '533',
  currencies: [ { code: 'AWG', name: 'Aruban florin', symbol: 'ƒ' } ],
  languages: 
   [ { iso639_1: 'nl',
       iso639_2: 'nld',
       name: 'Dutch',
       nativeName: 'Nederlands' },
     { iso639_1: 'pa',
       iso639_2: 'pan',
       name: '(Eastern) Punjabi',
       nativeName: 'ਪੰਜਾਬੀ' } ],
  translations: 
   { de: 'Aruba',
     es: 'Aruba',
     fr: 'Aruba',
     ja: 'アルバ',
     it: 'Aruba',
     br: 'Aruba',
     pt: 'Aruba',
     nl: 'Aruba',
     hr: 'Aruba',
     fa: 'آروبا' },
  flag: 'https://restcountries.eu/data/abw.svg',
  regionalBlocs: [],
  cioc: 'ARU' }
=======================================
=======================================
Result { name: 'Argentina',
  topLevelDomain: [ '.ar' ],
  alpha2Code: 'AR',
  alpha3Code: 'ARG',
  callingCodes: [ '54' ],
  capital: 'Buenos Aires',
  altSpellings: [ 'AR', 'Argentine Republic', 'República Argentina' ],
  region: 'Americas',
  subregion: 'South America',
  population: 43590400,
  latlng: [ -34, -64 ],
  demonym: 'Argentinean',
  area: 2780400,
  gini: 44.5,
  timezones: [ 'UTC-03:00' ],
  borders: [ 'BOL', 'BRA', 'CHL', 'PRY', 'URY' ],
  nativeName: 'Argentina',
  numericCode: '032',
  currencies: [ { code: 'ARS', name: 'Argentine peso', symbol: '$' } ],
  languages: 
   [ { iso639_1: 'es',
       iso639_2: 'spa',
       name: 'Spanish',
       nativeName: 'Español' },
     { iso639_1: 'gn',
       iso639_2: 'grn',
       name: 'Guaraní',
       nativeName: 'Avañe\'ẽ' } ],
  translations: 
   { de: 'Argentinien',
     es: 'Argentina',
     fr: 'Argentine',
     ja: 'アルゼンチン',
     it: 'Argentina',
     br: 'Argentina',
     pt: 'Argentina',
     nl: 'Argentinië',
     hr: 'Argentina',
     fa: 'آرژانتین' },
  flag: 'https://restcountries.eu/data/arg.svg',
  regionalBlocs: 
   [ { acronym: 'USAN',
       name: 'Union of South American Nations',
       otherAcronyms: [Array],
       otherNames: [Array] } ],
  cioc: 'ARG' }
=======================================
=======================================
Completed
=======================================

承諾數組

const endpointsRequest = [
        axios.get("https://restcountries.eu/rest/v2/name/aruba?fullText=true"),
        axios.get("http://localhost:8080"),
        axios.get("https://restcountries.eu/rest/v2/name/argentina?fullText=true")
    ];

回應

=======================================
Error: Error: connect ECONNREFUSED 127.0.0.1:8080
=======================================
  1. 如何重試承諾?

一旦穩定下來,Promise 本身就無法重試。 所以 Observable 從直接 Promise 開始也不能通過retry操作符重retry 一種方法是使用defer運算符,每次訂閱時都會創建新的承諾。

Rx.Observable.defer(() => return somePromise).retry(.....)
  1. 為什么如果一個承諾失敗另一個沒有得到解決?

forkJoin運算符的性質期望所有observable 都應該完成值發射。 要么失敗要么完成沒有價值,它會縮短執行時間。 為了使 forkJoin 發出完整的值,內部觀察(forkJoin 的參數)必須相應地處理以不出錯 / 並用值完成。

對於使用 RxJS 6+ 閱讀本文的任何人:以下代碼適用於我們。 MyClass.connect()在這種情況下返回一個 Promise。

defer(() => from(MyClass.connect()))
  .pipe(retry(3))
  .subscribe(...)

如果您想在某個條件下重試,也適用於 rxjs 6+

let loginStatus = defer(()=>{return from(new Promise(FB.getLoginStatus))}).pipe(
map((res:any)=>{
    if(res.status !== "connected"){
        throw res
    }
    return res
}),
retryWhen((error)=>{

    return error.pipe(
        delay(3000)
    )
})).subscribe({
next:(result:any)=>{
    console.log(result)
},
error:(err:any)=>{
    console.log(err)
}})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM