简体   繁体   English

如何在Angular2中进行数百次异步调用并相应地更新视图?

[英]How can I make hundreds of async calls in Angular2 and update view accordingly?

I want to perform hundreds of async HTTP Post requests (creating a new resource) in Angular 2 in a simple for loop and update an internal counter that is incremented by one as soon as one HTTP Post request successfully returns. 我想在一个简单的for循环中在Angular 2中执行数百个异步HTTP Post请求(创建新资源),并更新内部计数器,一旦一个HTTP Post请求成功返回,该计数器就会增加一。 I use rxjs Observables for handling the response from the backend. 我使用rxjs Observables处理来自后端的响应。

However, if I do that I can get into race conditions if two responses arrive simultaneously and are about to update the internal counter at the same time. 但是,如果我这样做,那么如果两个响应同时到达并且即将同时更新内部计数器,我将陷入竞争状态。 What is a proper solution in Angular2 and TypeScript for that? 为此,在Angular2和TypeScript中有什么合适的解决方案?

JS是单线程的(或更具体地说,是在单线程事件循环中运行),我认为您不必担心2个响应会同时更新一个变量。

答案很简单:浏览器中运行的JavaScript不能解决这个问题(因为Typescript始终会转换为JavaScript),因为它只有一个线程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM