如何避免链接的角度订阅中的HTTP调用

发布于 2025-01-20 01:58:02 字数 435 浏览 5 评论 0原文

我遇到过这样的情况:我调用 HTTP 服务来取回数据,然后根据该数据我需要立即进行另一个 HTTP 调用。通常我会将第一个输出通过管道传送到 switchMap 并完成。现在这是从路由订阅内部完成的,所以我不知道如何摆脱内部调用。

this.route.queryParamMap
  .pipe(switchMap(params => someService.get(params))
  .subscribe(x => {
    // do other things with x

    someService.getOtherThing(x.id).subscribe(...)
  })

get(params) 调用完成之前,我无法调用 getOtherThing(x.id)。如何避免在订阅中进行该服务调用?

I have a situation where I call an HTTP service to get data back, then based on that data I need to immediately make another HTTP call. Normally I'd pipe the first output to a switchMap and be done. That is now being done from inside a route subscription, so I'm not seeing how to get rid of the inner call.

this.route.queryParamMap
  .pipe(switchMap(params => someService.get(params))
  .subscribe(x => {
    // do other things with x

    someService.getOtherThing(x.id).subscribe(...)
  })

I can't call getOtherThing(x.id) until the get(params) call completes. How do I avoid that service call from within the subscription?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

奢欲 2025-01-27 01:58:02
this.route.queryParamMap.pipe(
  switchMap(params => someService.get(params)),
  concatMap((x) => {
    // do other things with x
    return someService.getOtherThing(x.id)
  })
).subscribe((response) => console.log('Your response'));

concatMap 将等待 someService.get(params) 完成才能触发 someService.getOtherThing(x.id)

this.route.queryParamMap.pipe(
  switchMap(params => someService.get(params)),
  concatMap((x) => {
    // do other things with x
    return someService.getOtherThing(x.id)
  })
).subscribe((response) => console.log('Your response'));

concatMap will wait for someService.get(params) to be completed in order to trigger someService.getOtherThing(x.id)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文