我有一个简单的效果,使用注入的角度服务发出一个GET请求。
该请求返回JSON响应( 10 000行)。
@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秒之间。不同的是,如果我使用getJsonResponse()使用POSTMAN调用相同的请求,那么请求总是占用2-3秒的时间。
getJsonResponse() {
return this.http
.get(`http://example.com/getjson`);
}在执行map之前需要15-20秒的原因是什么?
return new actions.OneExampleRestApiActionSuccess(response);这是因为响应是10000行的JSON对象,所以邮递员可以更快地处理它吗?我现在真的不知道什么是行阻滞剂:.getJsonResponse() to 返回新的actions.OneExampleRestApiActionSuccess(response);
因为正如我所说,如果我使用的是邮递员,服务器只需要2-3秒就能返回响应。
同样在上面的网络选项卡中,它显示了2秒的执行时间,但显然角或效果需要15-20秒才能到达返回新的。

发布于 2019-11-07 23:38:29
因为有了switchMap,你才有了比赛的条件。这导致了您在响应时间中看到的延迟。这里有一个很好的帖子,您可以参考
https://stackoverflow.com/questions/58753428
复制相似问题