Navigating transactional API calls in Angular 4 while handling state management

I am seeking guidance on how to efficiently execute the following task:

To add a user to our system through a form, I need to make 3 or 4 separate API calls/endpoints.

  • The first call will create a user account and return an ID.
  • The second call will generate a contact record using the previous ID from a different endpoint.
  • Subsequent calls may be needed to create additional business domain records based on the previous steps.

I have reviewed some discussions on executing multiple sequential requests: Multiple Sequential API calls in Angular 4

However, I am unsure of how to ensure that all the calls function as a single transaction. In case any step fails (such as invalid email regex in step 2), I want to handle the error and resume from where it stopped rather than restarting the entire process.

One idea I had was combining the solution provided in the reference with localStorage - but is there a more effective approach?

Thank you

Answer №1

To accomplish this task, one can utilize the concatMap, do, and merge operators in combination:

this.formSubmit$
  .concatMap(formData => this.http.post(...).map(user => user.id))
  .do(id => /* handle id processing here */)
  .merge(this.recoverWithId$) // continue from saved id point onwards

  .concatMap(id => this.http.post(...))
  .do(id => /* save necessary data here */)
  .merge(this.recoverWithWhatever$) // proceed from existing data onward

  ...
  .retry()
  .subscribe()

If any errors occur during the execution of this.http.post, they will be passed along and subsequent steps will be skipped. To prevent premature disposal, retry() is used before subscribe().

The use of the catch operator may also be considered depending on the desired behavior.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The module '...' is encountering an error with the imported value '...'. To resolve this issue, please include a @NgModule annotation

Implementing Angular. Process: Cloned a straightforward UI library to showcase the Angular package standards: https://github.com/jasonaden/simple-ui-lib Developed a new test application with ng new testApp npm link to the simple-ui-lib/dist Lin ...

Can you explain how to compare a value with a set of enum values?

How can you compare a value to a list of enum values, using TypeScript? For example, in Java: public enum Animal { CAT, DOG, BIRD, SNAKE public static EnumSet<Animal> Mammals = EnumSet.of(CAT, DOG); } Usage: Animal.Mammals.contains(myPet) ...

Issue with URL redirection and reloading in Safari and IE browsers

My current method for redirecting to the Item page and performing a full page reload works well in most browsers, but unfortunately not in Safari and IE. In those two browsers, the current page is reloaded instead. Does anyone have any suggestions for an ...

tips for working with vueJs and properties file

Hey, I was wondering if there's a way to store commonly used values and properties in a file like YML or a property file... ...and then reference them in VueJS components? In Java, we have Spring which allows us to use YML and property file properti ...

When transferring data from Django rest framework to a JavaScript array, there may be issues with missing

Struggling to retrieve data from my API endpoint using AJAX. During the iteration of the JSON object to populate my JavaScript array, I seem to be overlooking some values. Here's a breakdown: API data: HTTP 200 OK Allow: GET, HEAD, OPTIONS Conte ...

Refreshing a page in Angular 4/5 using TypeScript

I am currently working on a single-page application that utilizes routes for navigation: this.router.navigate(['/customer-home'], { skipLocationChange: true }); While on the customer home page, I have a feature to add a new customer via a REST ...

Issues with establishing connection to the database and handling input in Ionic are causing difficulties

I developed an Android app using Ionic. While everything seemed to work fine on my computer, I encountered a couple of issues when testing the app on my phone. The first issue is a bit more complex. I have data stored in a MySQL database, which I fetch us ...

PHP displays the array returned by Ajax as "[object Object],[object Object]"

When working with Jquery, I am creating two arrays - one embedded inside the other. Here is an example of what it looks like: arrayOne = [{name:'a',value:1}, {name:'b',value:2}] var arrayTwo = [{name:'foo',value:'blah&ap ...

Unwanted transparency issue in MaterialUI (MUI) BottomNavigation

Greetings fellow hobby developer! I recently embarked on a project using create-react-app and incorporated MUI dependencies. One feature I added was a fixed BottomNavigation, which you can view in action here. Interestingly, in CodeSandbox, the BottomNavi ...

$cookie retrieves information in the form of a string rather than a Map

Below is a snippet of my Javascript code. app.controller('RegisterCtrl', function($scope, $cookies) { $scope.registerInfo = {}; if($cookies.registerInfo){ angular.forEach($cookies.registerInfo, function(value, key) { $scope.registerInfo[ke ...

Transmitting multiple file attachments to a PHP file through AJAX

In my form, I have the following HTML code: <input type="file" id="attachments" name="attachments" multiple> I've already implemented a JavaScript function that handles form submission using AJAX, but it doesn't handle uploaded files. Wi ...

Looking for assistance with retrieving all files from a directory based on their file extensions in JavaScript

Looking for a way to gather all the '.txt' files from a folder that also contains a 'backup' folder, and moving them into the 'backup' folder using Node.js. If you have any suggestions, please share. Thanks! Best regards, M ...

Create functionality for editing and reviewing content within an HTML webpage

Looking to add edit and review modes to my webpage. In edit mode, users can drag and drop divs to different locations or resize them. Once changes are made, users may want to review their work. I have included a link for the user to switch to review mode ...

Flexbox is not properly repeating elements horizontally

I am struggling to align text boxes horizontally within ngFor loop, and I can't seem to pinpoint the mistake. Here is the HTML code from the parent component: <div class="maintenance-section"> <div class="yearly"> ...

Step by step guide on uploading files using Vue.js and Express.js

Hello there, I am new to Vuejs and Express and I'm looking to practice my skills. Currently, I am attempting to build a User Profile with an image upload feature using Vuejs and ExpressJs but unfortunately, none of the files or text are uploading as ...

Connecting ASP.NET Core null values with Angular2 null: A Step-by-Step Guide

Currently, I am working on a web application that is built using .NET Core and Angular 2. The issue I am facing is related to the currency parameter that I am sending from my database to Angular 2. Right now, the currency value is NULL, so Angular 2 receiv ...

Retrieve information from the API following a change in language with Ngx-Translate

I am working on a web app using Angular 12 that supports 3 languages. To manage the languages, I have utilized Ngx-Translate. Overall, everything is functioning correctly, but I have encountered a small issue. I am using the onLangChange event emitter to ...

Access a specific array containing a value within its name

I am currently working on developing a unique HTML Sudoku gameboard that dynamically loads different puzzles based on the PHP variables $_GET['level'] and $_GET['puzzle']. The PHP variables $get_level and $get_puzzle are assigned values ...

Testing a component that is utilizing data from an Angular ReplaySubject

I have been facing an issue with testing a component that relies on data from an EmployeeService. The EmployeeService utilizes Angular's ReplaySubject to fetch data from an api. While trying to test the component that subscribes to this ReplaySubject, ...

Steps for transforming a numerical value into an array with individual elements, such that the maximum value in the array will be 1

Can someone assist me? I have a value of 4.8 (it represents a rating of 4.8/5). Now I want to convert it into an array with the following structure: [1, 1, 1, 1, 0.8] What I mean is that the value should be split into 5 elements, with each element not ...