data() {
return {
// 数据准备
data: [1, 5, 3],
};
},
mounted() {
this.asyncMethod();
},
methods: {
async asyncMethod() {
let { data } = this;
for (let i = 0; i < data.length; i++) {
let result = await this.waitMinutes(data[i],i+1);
console.log(result);
i === data.length - 1 && console.log("所有操作均已完成!");
}
},
waitMinutes(time,i) {
console.log(`第${i}次开始:`, Math.round(new Date() / 1000));
console.log(`等待${time}秒`);
return new Promise(function (resolve, reject) {
// Do some asynchronous operations
setTimeout(() => {
resolve({ res: "完成等待", time: Math.round(new Date() / 1000) });
}, time * 1000);
});
},
},
运行结果: