gpt4 book ai didi

angular - 在 Rxjs 6 中使用 concat 和管道的正确方法是什么?

转载 作者:太空狗 更新时间:2023-10-29 17:56:16 26 4
gpt4 key购买 nike

我有一个可能返回 HTTP 202 的服务器调用。受 this SO 影响线程,我有以下内容:

this.http.get(url)
.pipe(
map(response => {
if (response.status === 202) {
throw response;
}
return response;
}),
retryWhen(errors => {
return errors.pipe(
delay(1000),
take(3),
concat(response => Observable.throw('Retries exceeded'))
);
}),
catchError(handleError)
);

获取有关使用 concatdeprecated 警告。我知道新的 concatrxjs 而不是 rxjs/operator 中。

但是,在这里使用新的 static concat 运算符的正确方法是什么?

找到以下 from this site

import { concat } from 'rxjs/operators';
a$.pipe(concat(b$, c$));

// becomes

import { concat } from 'rxjs';
concat(a$, b$, c$);

我无法理解在我的示例代码中连接了哪些 Observable

更新 1

将代码更改为:

return concat(this.http.get(url)
.pipe(
map(response => {
if (response.status === 202) {
throw response;
}
return response;
}),
retryWhen(errors => {
return errors.pipe(
delay(1000),
take(3)
);
}),
catchError(handleError)
), response => Observable.throw('Retries exceeded'));

但这会导致:

core.js:1598 ERROR TypeError: You provided 'function (response) { return rxjs__WEBPACK_IMPORTED_MODULE_2__["Observable"].throw('Retries exceeded'); }' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable.
at subscribeTo (subscribeTo.js:41)
at subscribeToResult (subscribeToResult.js:6)
at

最佳答案

管道运算符只是接受一个可观察对象并返回一个可观察对象的函数。所以你可以像这样使用 concat 工厂函数:

retryWhen(errors => {
return errors.pipe(
delay(1000),
take(3),
o => concat(o, throwError('Retries exceeded'))
);
})

此外,concat 运算符将被替换为/重命名为 concatWith。参见 this issue .

关于angular - 在 Rxjs 6 中使用 concat 和管道的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51566088/

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