vue同时请求多个接口,接口请求完成后在处理下一个方法(Promise.all用法)

1、Promise.all:

Promise .all()方法用于将多个 Promise 实例,包装成一个新的 Promise 实例。
在处理多个异步处理时非常有用,比如说一个页面上需要等两个或多个ajax的数据回来以后才正常显示。
需要特别注意的是,Promise.all获得的成功结果的数组里面的数据顺序和Promise.all接收到的数组顺序是一致的。

const p = Promise.all([p1, p2, p3]);

2、示例:

mounted(){
    
    
     this.geAllData()
},
methods: {
    
    
	//接口
	robotPoseWays(coordinateNum,toolNum,unitType){
    
    
         return new Promise((resolve, reject) => {
    
    
             this.$base.sendRobotCMD(
                 {
    
    }
             ).then((res) => {
    
    
                 resolve(res.data.result)
             }).catch((err) => {
    
    

             });
         });
     },
     //
     geAllData(){
    
    
     	Promise.all([
                    this.robotPoseWays(parseInt(this.nowPostureNum),-1,0), 
                    this.robotPoseWays(-1,parseInt(this.toolNumber),0)])
                .then(res => {
    
    
                  	let data ={
    
    	
                        endCoordinate: res[0],
                        toolPostition: res[1]
                    };
                    record(data).then((res) => {
    
    
                        if (res.success) {
    
    
                            
                        }
                    })
                }).catch(err=>{
    
    
                    console.log('robotPoseWays',err);
                })
     }
},

猜你喜欢

转载自blog.csdn.net/weixin_43883951/article/details/129861895