数组遍历完成设置data总是空

发布于 7 年前作者 junshen5976 次浏览最后编辑 7 年前来自 ask

打印squareLists是空

2 回复
lsong
lsong1 楼6 年前

getSquareList是个异步操作哦。你把 that.setData写到 squareItem[item.square_id] = res 的后面一排

xiuying50
xiuying502 楼5 年前

你需要这样写

async function yourHandleDataFunction() {
 
  if (res.statusCode == 200) {
    ...

 

const squareItems = {}


    await Promise.all(res.data.map((item, index) => async () {

     const data = await that.getSquareList(item.square_id)

      squareItems[item.square_id] = data
    })
 

    that.setData(...)

 }

 
}