vue同时请求多个接口,接口请求完成后在处理下一个方法(Promise.all用法)
Promise.all可以将多个Promise实例包装成一个新的Promise实例。同时,成功和失败的返回值是不同的,成功的时候返回的是一个结果数组,而失败的时候则返回最先被reject失败状态的值。
·
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);
})
}
},
更多推荐
已为社区贡献3条内容
所有评论(0)