以前很少在循环中执行ajax,今天突然遇到个场景用到。习惯使用map来遍历了,结果在map中遍历无法使用await方法。一时有点懵!!!后来发现普通的for循环中可以遍历。虽然不清楚为什么,但是问题总算是解决了。
错误信息 'await' expressions are only allowed within async functions and at the top levels of modules. 错误代码this.state.citys.map((item)=>{
await this.loadWetherInfo(item.location);
});
正确写法
直接使用for循环遍历就可以了。
const {citys} = this.state;
for (let i = 0; i
关注
打赏