首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >异步API调用问题:在Angular 6中迭代第一个响应并传递值以进行另一个API调用

异步API调用问题:在Angular 6中迭代第一个响应并传递值以进行另一个API调用
EN

Stack Overflow用户
提问于 2019-03-12 05:56:17
回答 1查看 33关注 0票数 0

我是java开发人员,试图使用异步API调用,这需要同步调用。我需要使第一个API调用>>迭代响应作为数组>>进行另一个API调用>>最后一个API调用的返回响应并订阅它。

以前,我使用switchmap实现了多个API调用,但不知道如何在迭代中使用switchmap或其他任何东西。

以下是我的代码

代码语言:javascript
复制
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;
                }
            });
        }));
        }         
}

和其他函数

代码语言:javascript
复制
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);
    }))
}
EN

回答 1

Stack Overflow用户

发布于 2019-03-12 06:02:23

您需要的是一个concatMap

“在前一个可观察对象完成之前,concatMap不会订阅下一个可观察对象”

有关参考,请参阅this

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55111010

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档