RxJS Observable:重试失败的Promise

时间:2018-01-10 20:02:06

标签: javascript rxjs observable

我一直在尝试创建一个解决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
=======================================

3 个答案:

答案 0 :(得分:3)

  1. 如何重试承诺?
  2. 承诺本身一旦定居就无法重试。所以Observable从直接Promise开始也无法通过retry运算符重试。一种方法是使用defer运算符,每次订阅时都会创建新的承诺。

    Rx.Observable.defer(() => return somePromise).retry(.....)
    
    1. 为什么承诺失败而另一个未得到解决?
    2. forkJoin运算符的性质期望所有 observable 完成值发射。任何一个失败或完成没有价值,它会短暂执行。为了使forkJoin发出完整的值,内部observalbe(forkJoin的param)必须相应地处理不错误/并使用值完成。

答案 1 :(得分:0)

对于使用 RxJS 6+ 阅读本文的任何人:以下代码适用于我们。在这种情况下,MyClass.connect() 返回一个 Promise。

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

答案 2 :(得分:0)

也适用于 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)
}})