我是 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);
}))
}