0

I have to recreate an object and for doing that I need to make 5 API call.

I make a first api call that in response has some id that ill use for the others 3 calls.

So I have something like this:

myFunction(id){
let apicall1 = this.service.apiCall1(id)
let object = {}
apicall1.pipe().subscribe(
  (data1: any) => {
   Object.assign(object, data1)
   let apicall2 = this.service.apiCall2(data1.id_customer)
   let apicall3 = this.service.apiCall3(data1.id_person)
   let apicall4 = this.service.apiCall4(data1.id_contact)
 
   apicall2.pipe().subscribe(
      (data2: any) => {Object.assign(object, data2)},
      (error2: any) = >{}
   )

   apicall3.pipe().subscribe(
      (data3: any) => {Object.assign(object, data3)},
      (error3: any) = >{}
   )

  apicall4.pipe().subscribe(
      (data4: any) => { 
        Object.assign(object, data4)
        let apicall5 = this.service.apiCall5(data4.id)
        apicall5.pipe().subscribe(
         (data5) => {Object.assign(object, data5)},
         (error5) => {}
        )
    },
      (error4: any) = >{}
   )
   
},
  (error1: any) => {}
)

// now after all the calls are done I need to pass to another function the object
 this.passToFunction(object)

}

I don't like how I make this api call and also I have a problem becasue how I wrote, the this.passToFunction pass only an object with data1.

How can I do this api call to have an object to pass to another function?

Jack23
  • 1,368
  • 7
  • 32

0 Answers0