RxJS 在第一次执行或使用错误谓词查找后不会执行管道运算符
我有这个可观察的订阅
this.tooltipEventSubject.pipe(
filter(event => event.type === "show"),
tap(() => this.tooltipContent.loading = true),
filter(() => this.messageContent?.sender !== undefined && this.tooltipContent.success === undefined),
mergeMap(() => this.jabService.searchEntityInAddressBook(this.messageContent?.sender ?? "")),
mergeMap(response => response.values),
filter(response => response !== undefined) as OperatorFunction<Entity | undefined, Entity>,
tap(() => console.log("before first")),
find(entity => entity.name === this.messageContent?.sender),
tap(() => console.log("after first")),
).subscribe({
next: () => console.log("next"),
error: err => console.log(err),
complete: () => console.log("complete")
})
它命中了“查找”谓词(使用调试器检查),然后什么都不做。
我尝试了很多运算符,比如首先跟随一个 catchError,在这个例子中我找到了。
它正在到达“首先”,但随后它不会打印任何其他内容。我知道 find 谓词返回 false,它应该返回 false。但为什么它停止运行呢?它不应该向以下运算符返回“未定义”吗?它甚至不应该打印“完整”吗?
谢谢您的回答
I have this observable subscribtion
this.tooltipEventSubject.pipe(
filter(event => event.type === "show"),
tap(() => this.tooltipContent.loading = true),
filter(() => this.messageContent?.sender !== undefined && this.tooltipContent.success === undefined),
mergeMap(() => this.jabService.searchEntityInAddressBook(this.messageContent?.sender ?? "")),
mergeMap(response => response.values),
filter(response => response !== undefined) as OperatorFunction<Entity | undefined, Entity>,
tap(() => console.log("before first")),
find(entity => entity.name === this.messageContent?.sender),
tap(() => console.log("after first")),
).subscribe({
next: () => console.log("next"),
error: err => console.log(err),
complete: () => console.log("complete")
})
It hits the "find" predicate (checked with debugger) and then does nothing.
I have tried a lot of operators, like first following by a catchError, here on this example I have find.
It is getting to "before first", but then it won't print anything else. I am aware that the find predicate is returning false, and it should. But why does it stop running ? Shouldn't it return "undefined" to the following operators ? And shouldn't it even print "complete" ?
Thank you for your answers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
find
运算符将等待谓词返回 true,直到那时它不会发出任何数据,即使是undefined
。仅当
source observable
completes 并且没有数据匹配时,find
才会发出undefined
。在您的情况下,源可观察对象是一个表单事件,因此除非您这样做,否则它不会完成。
Ex.takeUntil ..
我提供了两个示例。
例如。 1
输出:
例如。 2
在第二个示例中,它将在第 5 次单击后发出,或者如果取消注释
takeUntil
则在 5 秒后发出find
operator will wait until predicate returns true and till then it will not emit any data evenundefined
.find
will emitundefined
only whensource observable
completes and none of the data match.In your case source observable is a form event,So it will not be completed unless you do.
Ex.takeUntil ..
I am providing two example.
Ex. 1
Output:
Ex. 2
In 2nd example It will emit after 5th click or if you uncomment
takeUntil
then after 5 seconds如果您的谓词返回 false,则意味着没有与您的条件匹配的值,并且“查找”运算符在这种情况下不会返回任何值。
查看官方 rxjs 文档了解更多信息: https://rxjs.dev/api/operators/find
If your predicate returns false that means there's no value that matches your condition and the 'find' operator simply will not return any value in this case.
check official rxjs doc for more: https://rxjs.dev/api/operators/find