Home > front end >  What is the best way to implement sequencial http requests with optimistic concurrency in angular?
What is the best way to implement sequencial http requests with optimistic concurrency in angular?

Time:04-18

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. 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.

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?

Thanks!

CodePudding user response:

For creating sequential http request there is many way to handle it.

  1. you can use async await with any looping structure.
  2. you can use recursive function, (on each request callback call request function again, and manage all request to an array)
  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

hope you will get you solution here

CodePudding user response:

It's best to do it using rxjs, it gives you the best way to handle it.

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.

  • Related