我是Java开发人员,尝试使用需要同步调用的异步API调用。我需要进行第一个API调用>>将响应迭代为数组>>进行另一个API调用>>返回上一个API调用的响应并进行订阅
以前,我使用switchmap实现了多个API调用,但不知道如何在迭代中使用switchmap或其他任何功能。
这是我的代码
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;
}
});
}));
}
}
其他功能
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);
}))
}