[Fixed] Angular how to handle sequential requests and how handle loading var?


I save the profile and once the request would be sucess I make another request to create a payment. Right now the code lookes like this

      catchError(e => of(null))
    ).subscribe(res => {
      if (res) { // if user saved profile data
       const data = {
   params: {
            user: {
              surname: this.formInfo.get('surname').value,
              name: this.formInfo.get('name').value,
              father_name: this.formInfo.get('father_name').value,
              iin: this.formInfo.get('iin').value,
            tariff: obj ? {
              months: obj.Months,
              amount: obj.Amount,
              amountByMonth: obj.AmountByMonth
            } : null,
        this.buyService.create(data).pipe( ... 

What is the best way to make this code be better ? I have read about concatMap operator in RXjs but this is related to array of Observables , here I have only one response if user saved own data or not.
Also how should I handle loading variable here for each request spearately?


concatMap is the operator to use most of the times to concatenate 2 or more http operations.

So, in this case, your code could look like this

  concatMap(resp => {
     const data = {
        params: {
          // your params
     // the function passed to concatMap MUST return an Observable
     return this.buyService.create(data)
   next: result => {// manage the result},
   error: err => {// manage the error}

Honestly I do not understand when you write "concatMap operator in RXjs but this is related to array of Observables".

You may get some inspiration around typical http related use cases from this article.

Leave a Reply

(*) Required, Your email will not be published