ngrx效果中的竞争条件与switchmap导致性能降低

我有一个简单的效果,就是使用Injected Angular服务发出GET请求。

该请求返回JSON响应(万行)。

  @Effect()
  effectExample$ = this.actions$
    .pipe(
      ofType(actions.ONE_EXAMPLE_REST_API_actION),switchMap((action: actions.OneExampleRestApiaction) => {
        console.time('timer');
        return this.service
          .getJsonResponse()
          .pipe(
            map((response) => {
              console.timeEnd('timer');
              return new actions.OneExampleRestApiactionSuccess(response);
            })
          );
      })
    );

您可以看到我添加了 console.time('timer'); console.timeEnd('timer');

发生了什么事?

在控制台中,我得到的计时器大约在15到20秒之间。它会有所不同,但是如果我使用 POSTMAN 调用与getJsonResponse()调用的相同请求,则请求始终最多需要2-3秒。

getJsonResponse() {
return this.http
      .get(`http://example.com/getjson`);
}

执行地图或

之前需要15-20秒的原因可能是什么
return new actions.OneExampleRestApiactionSuccess(response);

是因为响应是10000行的JSON对象,所以邮递员可以更快地处理它吗?我现在真的不知道什么是线路拦截器: .getJsonResponse()返回新操作。OneExampleRestApiactionSuccess(响应);

因为服务器只需要2-3秒即可返回响应(如果我使用的是邮递员)。

也在上述请求的“网络”标签中,它显示了2秒的执行时间,但显然Angular或Effect需要15-20秒才能获得返回新动作。OneExampleRestApiactionSuccess(response); >

ngrx效果中的竞争条件与switchmap导致性能降低

linguohan 回答:ngrx效果中的竞争条件与switchmap导致性能降低

由于switchMap,您正在达到竞赛条件。这会导致您在响应时间内看到延迟。 这是您推荐的一个不错的post

本文链接:https://www.f2er.com/3142990.html

大家都在问