首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >ngrx效果-组合两个API

ngrx效果-组合两个API
EN

Stack Overflow用户
提问于 2020-05-25 02:39:21
回答 1查看 88关注 0票数 0

下面的效果是使用update api更新对象的一部分,然后通过findById api获取整个对象,所以我使用forkJoin来组合这两个api,但我希望findById api在update api的1秒后执行,所以我使用delay(1000),但它不起作用。

代码语言:javascript
运行
复制
@Effect()
updateGeographicScope$ = this.actions$.pipe(
    ofType<conventionsActions.PatchGeographicScope>(conventionsActions.ConventionActionTypes.PATCH_GEOGRAPHIC_SCOPE),
    map(action => action.payload),
    exhaustMap(geographicScope => forkJoin(this.apiConvention.update(geographicScope),
        this.apiConvention.findById (geographicScope.externalId).pipe(delay(1000))).pipe(
            map(([first, convention]) => new conventionsActions.PatchSuccess({
                id: convention.externalId,
                changes: convention
            })),
            catchError(err => {
                console.error(err.message);
                return of(new conventionsActions.Failure({ concern: 'PATCH', error: err }));
            })
        ))
);
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-05-25 03:11:59

为此,您需要使用concattimer。对于concat,它是在开始下一个流之前要完成的第一个流。因此,它进行更新,然后等待1秒,然后进行findById。

代码语言:javascript
运行
复制
@Effect()
updateGeographicScope$ = this.actions$.pipe(
    ofType<conventionsActions.PatchGeographicScope>(conventionsActions.ConventionActionTypes.PATCH_GEOGRAPHIC_SCOPE),
    map(action => action.payload),
    mergeMap(geographicScope => concat(
      this.apiConvention.update(geographicScope).pipe(switchMapTo(EMPTY)), // makes a request
      timer(1000).pipe(switchMapTo(EMPTY)), // waits 1 sec
      this.apiConvention.findById(geographicScope.externalId), // makes a request
    )),
    map(convention => new conventionsActions.PatchSuccess({
      id: convention.externalId,
      changes: convention
    })),
    catchError(err => {
      console.error(err.message);
      return of(new conventionsActions.Failure({ concern: 'PATCH', error: err }));
    }),
    repeat(), // make active after a failure
  )),
);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61990656

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档