async和await是ES7引入的新语法,能够更加不便的异步操作
async关键字用在函数上(async函数的返回值是promise实例对象)
await必须跟async同时应用。
例子:
// 获取数据1 getDatas1() { return new Promise((resolve, reject) => { this.$http .get(window.SITE_CONFIG.apiURL + "/static/queryStatic", { params: { dictType: "accept_promotion", }, }) .then((res) => { let result = res.body.data; this.ycslArr1 = result; resolve("嘿嘿嘿1") }) .catch(() => {}); }); }, // 获取数据2 getDatas2() { return new Promise((resolve, reject) => { this.$http .post(window.SITE_CONFIG.apiURL + "/service656", { machineCode: "SfJjeyP0xmnS6h8R", url: "/power/getItemCountByOnlineDepth/1.0", postdata: {}, }) .then((res) => { let result = JSON.parse(res.body.result).data; this.wbsdData2.pop(); resolve("嘿嘿嘿222"); }) .catch(() => {}); }); }, async query() { let ret1 = await this.getDatas1(); var ret2 = await this.getDatas2(); console.log("ret1????", ret1, ret2); // 嘿嘿嘿1,嘿嘿嘿222 },