Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RxJS Observable: retry a Promise that fail

I have been trying to create a functionality that resolve 3 promises, I any of this promises fails, I want to retry it. My idea is: if the second promise fail, the 1st and 3rd promise should be resolved anyway the suscriptor could be notified.

that is my code:

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("=======================================");
        }
    );

It is possible achieve this?

example of response if using this promises array:

Promises Array

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

Response

=======================================
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
=======================================

Promises Array

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")
    ];

response

=======================================
Error: Error: connect ECONNREFUSED 127.0.0.1:8080
=======================================
like image 751
Gonzalo Pincheira Arancibia Avatar asked Nov 25 '25 00:11

Gonzalo Pincheira Arancibia


2 Answers

  1. How to retry promise?

Promise itself can't be retried once settled down. So Observable starts from direct Promise also can't be retried via retry operator. One way is using defer operator, creating new promise each time it subscribes into.

Rx.Observable.defer(() => return somePromise).retry(.....)
  1. Why if a promise fail the another are not resolved?

Nature of forkJoin operator expects all observable should complete with value emission. Either one fails or completes without value, it short-curcuits execution. To make forkJoin emits completed values, inner observalbe (param of forkJoin) must handle accordingly to not error / and complete with values.

like image 127
OJ Kwon Avatar answered Nov 27 '25 13:11

OJ Kwon


For anyone reading this who's using RxJS 6+: the following code works for us. MyClass.connect() returns a Promise in this case.

defer(() => from(MyClass.connect()))
  .pipe(retry(3))
  .subscribe(...)
like image 41
Dennis Ameling Avatar answered Nov 27 '25 15:11

Dennis Ameling



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!