gpt4 book ai didi

javascript - RxJS 可观察到的 : retry a Promise that fail

转载 作者:行者123 更新时间:2023-11-29 18:58:07 32 4
gpt4 key购买 nike

我一直在尝试创建一个解决 3 个 promise 的功能,但我的任何一个 promise 都失败了,我想重试。我的想法是:如果第二个 promise 失败,那么第一个和第三个 promise 应该被解决,无论如何可以通知订阅者。

这是我的代码:

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

这有可能实现吗?

使用此 promise 数组的响应示例:

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

Promises 数组

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?

一旦安顿下来,Promise 本身就无法重试。所以 Observable 直接从 Promise 开始,也不能通过 retry 运算符重试。一种方法是使用 defer 运算符,在每次订阅时创建新的 promise 。

Rx.Observable.defer(() => return somePromise).retry(.....)
  1. 为什么如果一个 promise 失败了,另一个 promise 却没有得到解决?

forkJoin 运算符的性质要求所有 observable 应该完成并发出值。要么失败要么没有值(value)地完成,它会缩短执行时间。为了使 forkJoin 发出完整的值,内部 observalbe(forkJoin 的参数)必须相应地处理以不出错/并完成值。

关于javascript - RxJS 可观察到的 : retry a Promise that fail,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48195191/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com