Vue用axios实现同步

很多情况下我们需要用前一个函数的返回值在继续,但是axios是异步调用的,不做任何操作的话是异步的,没办法实现我们想要的,那下面介绍如何实现同步调用

利用async 和 await实现

async getpodDetai(scope){
            //先获取container名字
            this.podDetailData.params.namespace = this.namespaceValue
            this.podDetailData.params.pod_name = scope.row.metadata.name
            await httpClient.get(this.podDetailData.podDetailUrl,{params:this.podDetailData.params}).then(
                res => {
                    //console.log(res.data.spec.containers[0].name)
                    this.container_name = res.data.spec.containers[0].name
                }
            ).catch(
                res => this.$message.error({
                message: res.msg
                })
            )
        },
        async getpodLogs(scope){
           await  this.getpodDetai(scope) //等获取容器name,在去获取日志
            console.log(this.container_name)
            this.scaleDialog = true
            this.podLogData.params.container_name= this.container_name
            this.podLogData.params.namespace = this.namespaceValue
            this.podLogData.params.pod_name = scope.row.metadata.name
            httpClient.get(this.podLogData.podLogUrl,{params:this.podLogData.params}).then(
                res => {
                    console.log(res.data)
                    this.logContent = res.data
                }
            ).catch(
                res => this.$message.error({
                    message: res.msg
                })
            )
        },
posted on 2022-06-12 21:07  每天进步一点点点点点  阅读(3275)  评论(0编辑  收藏  举报