rxjs让两个多或者多个Observable完成之后再执行下一步

最新有个需求,需要在两个获取配置code的请求完成之后再去执行下一步,以往的做法可能是写链式,就是一个套在另外一个的subscribe里面,但是那样太丑了,而且效率也不高,突然想起来之前看到过forkJoin,记录下。

可以看到我去call了两个 code ,等这两个都执行完了,再去执行我的附加代码 

  const getEventTypeCodes = this._AIService.getCodesByCodeTypePk({ codeTypePk: 'xxxx', isExternal: false })
  .pipe(
    map(item => item.filter(res => res['locale'] === this.lang))
  );
  const getEventSourcecCodes = this._AIService.getCodesByCodeTypePk({ codeTypePk: 'xxxxx', isExternal: false })
  .pipe(
    map(item => item.filter(res => res['locale'] === this.lang))
  );
  forkJoin([getEventTypeCodes, getEventSourcecCodes]).subscribe(
    ([eventype, eventsource]) => {
      this.eventTypeCode = eventype;
      this.eventSourceCode = eventsource;
      this.getLatestData();
    });

你可能感兴趣的:(Angular,JS&TS,angular)