尽管内部有错误,如何从第一个可观察到的数据 return?
How to return data from first observable despite error inner?
我有两个流一一:
.pipe(
map(() => new SemanticObject(this.objectLayer, this.map)),
concatMap((semanticObject: SemanticObject) =>
from(semanticObject.saveObject(semanticObject.getObject())).pipe(
concatMap(() => semanticObject.changeObjectStateUnom()),
map((data) => data;
),
),
)
我尝试 return 来自此处第一个流的数据 map((data) => data;
。尽管 concatMap 失败了。怎么做?
我试过这个:
.pipe(
tap(() => this.loading$.next(false)),
filter(Boolean),
map(() => new SemanticObject(this.objectLayer, this.map)),
concatMap((semanticObject: SemanticObject) =>
from(semanticObject.saveObject(semanticObject.getObject())).pipe(
concatMap((updatedObject: ObjectLayer) => semanticObject.changeObjectStateUnom().pipe(
catchError((updatedObject) => updatedObject),
mapTo(updatedObject)
)),
),
),
)
你太接近了! CatchError
是一个高阶运算符(类似于 concatMap,但用于错误),因此您需要 return 一个可观察对象。
例如:
pipe(
tap(() => this.loading$.next(false)),
filter(Boolean),
map(() => new SemanticObject(this.objectLayer, this.map)),
concatMap((semanticObject: SemanticObject) =>
from(semanticObject.saveObject(semanticObject.getObject()))
),
concatMap((updatedObject: ObjectLayer) =>
semanticObject.changeObjectStateUnom().pipe(
catchError(error => of("Error Token")),
map(changedState =>
changedState === "Error Token" ?
updatedObject : changedState
)
)
)
);
当然,使用字符串作为错误标记并不是惯用的,但您可以选择最适合您的方式。
我有两个流一一:
.pipe(
map(() => new SemanticObject(this.objectLayer, this.map)),
concatMap((semanticObject: SemanticObject) =>
from(semanticObject.saveObject(semanticObject.getObject())).pipe(
concatMap(() => semanticObject.changeObjectStateUnom()),
map((data) => data;
),
),
)
我尝试 return 来自此处第一个流的数据 map((data) => data;
。尽管 concatMap 失败了。怎么做?
我试过这个:
.pipe(
tap(() => this.loading$.next(false)),
filter(Boolean),
map(() => new SemanticObject(this.objectLayer, this.map)),
concatMap((semanticObject: SemanticObject) =>
from(semanticObject.saveObject(semanticObject.getObject())).pipe(
concatMap((updatedObject: ObjectLayer) => semanticObject.changeObjectStateUnom().pipe(
catchError((updatedObject) => updatedObject),
mapTo(updatedObject)
)),
),
),
)
你太接近了! CatchError
是一个高阶运算符(类似于 concatMap,但用于错误),因此您需要 return 一个可观察对象。
例如:
pipe(
tap(() => this.loading$.next(false)),
filter(Boolean),
map(() => new SemanticObject(this.objectLayer, this.map)),
concatMap((semanticObject: SemanticObject) =>
from(semanticObject.saveObject(semanticObject.getObject()))
),
concatMap((updatedObject: ObjectLayer) =>
semanticObject.changeObjectStateUnom().pipe(
catchError(error => of("Error Token")),
map(changedState =>
changedState === "Error Token" ?
updatedObject : changedState
)
)
)
);
当然,使用字符串作为错误标记并不是惯用的,但您可以选择最适合您的方式。