- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直在尝试创建一个解决 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
=======================================
最佳答案
一旦安顿下来,Promise 本身就无法重试。所以 Observable 直接从 Promise 开始,也不能通过 retry
运算符重试。一种方法是使用 defer
运算符,在每次订阅时创建新的 promise 。
Rx.Observable.defer(() => return somePromise).retry(.....)
forkJoin
运算符的性质要求所有 observable 应该完成并发出值。要么失败要么没有值(value)地完成,它会缩短执行时间。为了使 forkJoin 发出完整的值,内部 observalbe(forkJoin 的参数)必须相应地处理以不出错/并完成值。
关于javascript - RxJS 可观察到的 : retry a Promise that fail,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48195191/
RxJS 中是否有一个运算符可以让我缓冲项目并在信号可观察对象触发时将它们一个一个地放出?有点像bufferWhen,但不是在每个信号上转储整个缓冲区,而是每个信号转储一定数量。它甚至可以转储信号 o
我正在像这样创建我的可观察源(每 5 秒调用一次 api): const obs$ = Observable.interval(5000).switchMap(() => makeApiCall())
我有一个 Action ,然后将触发一个ajax请求。 如果由于某种原因操作失败,我什么也不想做。我没有执行可以执行的无操作功能,而不是创建只返回先前状态的空白操作? export default f
在以下代码中:- RxJS.Observable.of(1,2).first().subscribe((x) => console.log(x);); 给定运营商 first() 是否有必要取消订阅?
我有一种情况,可以在很短的时间内将很多事件发送到流中。我想要一个运算符,它是ojit_code和debounceTime的混合体。 以下演示可用于说明我想拥有的https://stackblitz
我的用例如下:我得到事件,有时会突然发生。如果发生突发,我只需要处理一次即可。去抖动会执行此操作。 但是,去抖动仅给我提供连拍的最后一个元素,但我需要了解连拍中的所有元素才能汇总(使用平面图)。 这可
简化以下代码示例的方法是什么? 我找不到合适的运算符..有人可以举一个简短的例子吗? this.returnsObservable1(...) .subscribe( success =>
在RxJS 6中,如何导入静态合并功能以合并Observable列表? 我希望能够做到: const merged$ = merge( obs1$, obs2$, obs3$
我正在阅读 RxJS 的官方文档,然后我意识到它们都在做完全相同的事情。 对我来说,它们看起来完全相似。 如果有区别请指出。 最佳答案 我将根据它们的 Time 版本来描述它们之间的区别,因为这是我最
我对基本的 RxJS 概念有点熟悉,比如 Observables、Observers 和 Subjects,但是 RxJS Notifications概念对我来说是全新的。 它有什么用?我应该什么时候
从 rxjs 6.5 切换到 rxjs 7 后,我遇到了这个奇怪的错误。我不确定这是 rxjs 7 的类型问题还是 stackblitz ( https://stackblitz.com/edit/r
以前我只能使用此代码导入使用过的运算符: import 'rxjs/Observable'; import 'rxjs/add/operator/map'; import 'rxjs/add/oper
combineLatest 函数可以从 rxjs 和 rxjs/operators 导入。 当我从 rxjs/operators 导入它时(就像我导入 combineAll 我收到以下错误: TS23
我有一系列事件通过 fromEventPattern 进行像这样: fromEventPattern(addEventHandler).subscribe(ps$); 由于业务怪癖,我预计有时会抛出异
我是 rxjs 的新手,无法解决这个问题: 我有两个流: 一个有传入的对象 ---a----b----c----d-----> 一个是从列表中选择的对象 ----------------c---->
如果一个 observable 完成,我是否仍然需要取消订阅/处置(在 RxJS 中)该 observable 以删除 Observer(防止内存泄漏),或者一旦 onComplete 或 onErr
我有这样的订阅: this.test.subscribe(params => { ...some code }); 如果我传递回调函数而不是箭头函数,则缺少上下文。 我想将上下文绑定(bind)到
我有一个可观察的: messages: string[] = ['a', 'b', 'c']; const source = from(messages) 你如何延迟它,所以当有人订阅它时,它
我可以让 observable 触发一次该值。但我希望它在变量的值发生变化时发生。实际上我需要一个观察者。这就是我认为 observable 的意义所在。观察事物的值(value)或状态并更新订阅它的
我有以下代码: const fetchBook = (bookId: number) => { const title = 'Book' + bookId; console.log('
我是一名优秀的程序员,十分优秀!