代码如下:
`
async handle_freeze(val){
if(this.selectedData.length===0){return this.$message({type:"warning",message:'请选择账号'})}
const _this=this;
function init_ids(){
return new Promise((succ) => {
let ids = [];
_this.selectedData.forEach(async item => {
let res = await _this.$http(api_get_account_site_detail, item.id);
res.data.forEach(data => {
ids.push(data.id);
});
});
ids = ids.join(",");
succ(ids);
});
}
const ids=await init_ids();
}`
大致逻辑就是在一个forEach循环中不断发起异步请求,最后将所有请求结果拼接成一个字符串。实际运行却发现`
const ids=await init_ids()这一句并没有被await阻塞,而是直接运行并返回了undefine,我猜测应该是init_ids这个方法写的有问题,奈何我对async/await的使用经验并不多,看不出哪里错了,只好请教大佬了。。
改用for循环或者for..of