想要await按照預期來執行,就必須是promise
methods: {
getAll() {
return new Promise((resolve, reject) => {
this.$axios.get(eventApi.getAllUsers).then(response => {
let a = response.data[0];
console.log(a);
resolve(a);
});
});
},
getlog() {
return new Promise((resolve, reject) => {
console.log("111");
resolve("111");
});
},
getLog2() {
console.log("222");
},
getlog3() {
setTimeout(() => {
console.log("333");
}, 100);
},
getlog4() {
return new Promise((resolve, reject) => {
setTimeout(() => {
console.log("444");
resolve();
}, 1000);
});
},
async getAllData() {
await this.getlog();
await this.getlog4();
await this.getAll();
await this.getlog3();
await this.getLog2();
}
複製程式碼
結果如下
不理解的請留言!哈哈哈,我會告訴你?