简体   繁体   English

在 angular 中实现具有乐观并发的顺序 http 请求的最佳方法是什么?

[英]What is the best way to implement sequencial http requests with optimistic concurrency in angular?

In this Angular app, the user fills inputs on a table, after each change, the app makes an http request to save changes, with each request, it sends a number which indicates the concurrency version.在这个 Angular 应用程序中,用户在表格中填写输入,每次更改后,应用程序都会发出 http 请求以保存更改,对于每个请求,它都会发送一个表示并发版本的数字。 If the process is successful the server sends a response with the new version.如果该过程成功,服务器将发送带有新版本的响应。

The problem comes when the user makes changes too fast, the post request will be sent before the previous response is received and therefore the concurrency number will not match.问题来了当用户更改太快时,post 请求将在收到之前的响应之前发送,因此并发数将不匹配。

I am new to angular, so, what is the best way to make a service that can send the requests one after another (one at a time) in a way that this allows me to save the concurrency version from a response to be used in the next request?我是 angular 的新手,所以,制作可以一个接一个(一次一个)发送请求的服务的最佳方式是什么,这样我就可以从响应中保存并发版本以用于下一个要求?

Thanks!谢谢!

It's best to do it using rxjs, it gives you the best way to handle it.最好使用 rxjs 来完成,它为您提供了处理它的最佳方式。

You can follow this , and you can use the appropriate operators based on your needs and requirements.你可以按照这个,你可以根据你的需要和要求使用合适的运算符。

By reading this blog , you'll gain a greater understanding of rxjs.通过阅读这篇博客,您将对 rxjs 有更深入的了解。

For creating sequential http request there is many way to handle it.要创建连续的 http 请求,有很多方法可以处理它。

  1. you can use async await with any looping structure.您可以将 async await 与任何循环结构一起使用。
  2. you can use recursive function, (on each request callback call request function again, and manage all request to an array)您可以使用递归 function,(在每个请求上再次回调调用请求 function,并将所有请求管理到一个数组)
  3. you can use rxjs constuctMap() function, here is an link for how to use it https://riptutorial.com/rxjs/example/28035/sending-multiple-sequential-http-requests您可以使用 rxjs constuctMap() function,这里是如何使用它的链接https://riptutorial.com/rxjs/example/28035/sending-multiple-sequential-http-requests

hope you will get you solution here希望你能在这里得到解决方案

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

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