解决map(), forEach()里面使用异步函数,使用await接收无效时

使用过两种解决方案:

1是用原始的for循环

 for(let i=0;i<_this.dataShow.length;i++){
_this.dataShow[i].MarkContent = await _this.$curl.get(_this.dataShow[i].MarkContentUrl,{contentType: "application/x-www-form-urlencoded; charset=utf-8"});
 }

2坚持使用forEach,map

let newarr=await Promise.all(arr.map((e)=>{
    await 请求
  return **** }))

原理应该是foreach内部封装了while,循环并行执行,而且并行执行数组的所有callback函数,不会等待里面的callback的返回。

 

posted @ 2020-05-21 09:48  Jennyishere  阅读(1967)  评论(1编辑  收藏  举报