js 在循环事件中使用await

js yekong 1218℃

js在进行for循环请求接口时,为了避免数据混乱,需要接口按次序请求接口

async getImgListByKey () {
  try {
    var that = this
    for (const key of that.list) {
      var indexs = 0
      that.list.forEach((type, index) => {
        if (key.id === type.id) {
          indexs = index
        }
      })
      
      await that.getDeviceRegs(indexs, that.list[indexs].id)
    }
  } catch (error) {
    console.log(error)
  }
},

调整

2022年08月10日 因为使用for (const key of that.list)循环时,不知道怎么获取index所以,干脆在循环体内,二次循环重新获取index的值。

喜欢 (0)