【问题标题】:Rx Observable pipe handling errorsRx Observable 管道处理错误
【发布时间】:2018-04-30 13:32:53
【问题描述】:

我有一个submit 例程,其中有一个before“回调”和一个after“回调”。

beforesubmitafter 都可以向后端服务执行异步请求。

这三个例程应该按顺序调用,并包裹在一个 zip 运算符中,以便仅在三个例程完成后执行进一步的代码。

作为新手 Rx 程序员,我不确定如何将三个例程“管道”在一起

更新 1

例行程序是指函数。

submit() { // same for 'before' and 'after'
  this.backend.methodMakingHttpRequestAndReturningObservable();
}

如问题标题所述,问题出在这种特殊情况下 Angular 内部使用的 Rx Observable 的上下文中。

【问题讨论】:

  • 例程和回调是什么意思?可以举个例子吗?
  • 到目前为止你有什么尝试?
  • 什么是“之前”“之后”?事件 ?可观察?还要别的吗 ?我们需要更多代码才能为您提供帮助

标签: angular rxjs observable reactive-programming


【解决方案1】:

concatMap 不订阅下一个 observable 直到前一个完成。 (Check this site)。

import { concatMap } from 'rxjs/concatMap';

$before = httpRequest();
$submit = httpRequest();
$after = httpRequest();

const $request = $before.pipe(
  concatMap(beforeResponse => {
    // use beforeResponse whatever you want
    return $submit;
  },
  concatMap(submitResponse => {
    // use submitResponse whatever you want
    return $after;
  })
);

$request.subscribe(
  res => console.log(res),
  err => console.log(`something wrong happen: ${err}`) // if any error occur, this is the place to handle it.
);

【讨论】:

  • 谢谢,我今天试试这个方法
猜你喜欢
  • 2019-04-12
  • 2016-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-24
  • 2013-12-10
  • 2015-08-26
相关资源
最近更新 更多