0

I am java developer, trying to work with Asynchronous API calls which needs to be called synchronously. I need to make first API call >> iterate response as array >> make another API call >> return response of last API call and subscribe it

Previously, i achieved multiple API calls using switchmap but has no idea how to use switchmap or anything else in iterations.

Here is my Code

multipleAction(actionServiceName,host,hanger,toDoAction) {
 .........       

    let getServiceUrl = `/${proxy}/services/${actionHostName}/`;

    if(toDoAction=="stopservice"){
            return this.http.get(getServiceUrl,httpOptions)
    .pipe(map((output:Response) => { 
            let array = JSON.parse(JSON.stringify(output));
            array.forEach(element => {
                if(element.serviceName==actionServiceName && element.status=="Up")
                {
                    console.log("stopping service");
         // Here is issue. Trying to achieve something like this, being backend developer. i know this API call doesnt work in Angular
                    let res = this.startStopService(proxy,toDoAction,actionHostName,actionServiceName,element.serviceVersion,httpOptions);
                    console.log("step 2:",res);
                 //need this value to be subscribed
                    return res;
                }
            });
        }));
        }         
}

And Other function

startStopService(proxy,toDoAction,actionHostName,actionServiceName,serviceVersion,httpOptions){ 

    let getStatusUrl = `/${proxy}/servicestatus/${actionHostName}/${actionServiceName}/CSS/${serviceVersion}`;
    let serviceActionUrl = `/${proxy}/${toDoAction}/${actionHostName}/${actionServiceName}/CSS/${serviceVersion}`;

//these two API calls works fine this function is called separately. but doesnt work synchronously when with previous function

    return this.http.get(getStatusUrl,httpOptions)
    .pipe(map((output) => { 
        return output;
    }),
    switchMap(output =>
    this.http.get(serviceActionUrl
    +JSON.parse(JSON.stringify(output)).statusDescription,
    httpOptions)),        
    tap(output2 => {  
        console.log(JSON.parse(JSON.stringify(output2)).statusDescription);
    }))
}
ronypatil
  • 153
  • 2
  • 3
  • 19

1 Answers1

1

What you need is a concatMap.

"concatMap does not subscribe to the next observable until the previous completes"

See this for reference.

robert
  • 5,742
  • 7
  • 28
  • 37