RxJS Observable:重试失败的 Promise

Gon*_*bia 2 javascript observable rxjs

我一直在尝试创建一个解决 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("=======================================");
        }
    );
Run Code Online (Sandbox Code Playgroud)

有可能实现这一目标吗?

使用此 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")
    ];
Run Code Online (Sandbox Code Playgroud)

回复

=======================================
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
=======================================
Run Code Online (Sandbox Code Playgroud)

承诺数组

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")
    ];
Run Code Online (Sandbox Code Playgroud)

回复

=======================================
Error: Error: connect ECONNREFUSED 127.0.0.1:8080
=======================================
Run Code Online (Sandbox Code Playgroud)

小智 7

  1. 如何重试承诺?

一旦稳定下来,Promise 本身就无法重试。所以 Observable 从直接 Promise 开始也不能通过retry操作符重试。一种方法是使用defer操作符,每次订阅时创建新的承诺。

Rx.Observable.defer(() => return somePromise).retry(.....)
Run Code Online (Sandbox Code Playgroud)
  1. 为什么如果一个承诺失败另一个没有得到解决?

forkJoin运营商的本质期望所有的observable 都应该以价值发射完成。要么失败要么完成没有价值,它会缩短执行时间。为了使 forkJoin 发出完整的值,内部观察(forkJoin 的参数)必须相应地处理以不出错/并用值完成。