自学网站免费,制作网站深圳,西安网站优化效果,漳州招商局规划建设局网站vue在一个方法执行完后执行另一个方法
用Promise.all来实现。 Promise是ES6的新特性#xff0c;用于处理异步操作逻辑#xff0c;用过给Promise添加then和catch函数#xff0c;处理成功和失败的情况
ES7中新提出async搭配await#xff0c;建议使用async搭配await。
func…vue在一个方法执行完后执行另一个方法
用Promise.all来实现。 Promise是ES6的新特性用于处理异步操作逻辑用过给Promise添加then和catch函数处理成功和失败的情况
ES7中新提出async搭配await建议使用async搭配await。
function fun1(){return new Promise((resolve, reject) {/* 你的逻辑代码 */console.log(1);});
},
function fun2(){return new Promise((resolve, reject) {/* 你的逻辑代码 */console.log(2);});
},
function fun3(){return new Promise((resolve, reject) {/* 你的逻辑代码 */console.log(3);});
},
/* 调用 */
function run(){Promise.all([this.fun1(),this.fun2(),this.fun3()]).then(res {/* 你的逻辑代码 */console.log(run);})
}
async/await使用方法
//示例1async function testSync() {const response await new Promise(resolve {setTimeout(() {resolve(async await test...);}, 1000);});console.log(response);}testSync();//async await test...//示例2async getScheduleList(selectDate) {let response;// 这里request为向服务的发请求的方法await request(api.getScheduleList, {date: selectDate}).then(res {response res;});return response
}init() {this.getScheduleList(selectDate).then(res {console.log(res)}
}//示例3swichMenu: async function() {//点击其中一个 menuconst num await getNum()return num
}swichMenu().then(res {console.log(res)
})