我是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);
}))
}发布于 2019-03-12 06:02:23
https://stackoverflow.com/questions/55111010
复制相似问题