Issue
I am making two api calls in my angular code (1st api is POST and 2nd is PUT) and i want to make sure either both of them successfully executes or none of them.
Scenarios :
- Meaning if the first 1st api call fails, call to 2nd api should not be made and should show the error for the first 1st api only.
- IMPORTANT CASE : If the 1st api executes successfully and 2nd api fails, the 1st api call that was made should be rejected and i should show error for 2nd api (or to say none of them should get called)
//Just a example call for api
this.movementService.saveMovementDetails(movData).subsribe()
this.tourService.saveTourDetails(tourId,tourData).subsribe()
So far i have tried mergemap, forkJoin in RxJS but i am not able to figure out how will I be able to achieve this. Any help will mean a lot to me!
Solution
This is actually a really hard problem. it's a distributed transaction which can be really difficult to solve. I would recommend not doing it in the client. You really can't trust a browser to handle this. You won't be able to just cancel the api call either, you will need to make an additional api call to rollback the api call that succeeded. I would highly suggest trying to make this into one api call/transaction if it's possible.
If for some reason you can't make it one api call and one transaction, I would suggest moving this logic onto the server and using the saga pattern where each step in the process can be rolled back if a step later in the process fails. You can learn more about it here
Answered By - Jimeh
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.