For loop API request, how to know which one of the current requests is successful after success

after the circular request, the es6 syntax is not supported, and it is not passed to the server json format. The API will only return the success status without any identification. How to know which array element is passed successfully at the front end. Thank you

var arr = [{
  gymAddr: "00000000",
  trainingActionId: "123",
  deviceId: "1"
}, {
  gymAddr: "00000000",
  trainingActionId: "123",
  deviceId: "2"
}, {
  gymAddr: "00000000",
  trainingActionId: "123",
  deviceId: "3"
}]

for (var i = 0; i < arr.length; iPP) {
  wx.request({
    url: "https://localhost:8080/trainingRecord/single",
    data: arr[i],
    method: "POST",
    header: {
      "content-type": "application/x-www-form-urlencoded"
    },
    success: function(res) {
      console.log("res", res);
      console.log(this)
    }
  })
}
Apr.02,2021

for (var i = 0; i < arr.length; iPP) {
    (function(){
        var _i=i;
        wx.request({
        ...
        })
    })()
}

var arr = [{
  gymAddr: "00000000",
  trainingActionId: "123",
  deviceId: "1"
}, {
  gymAddr: "00000000",
  trainingActionId: "123",
  deviceId: "2"
}, {
  gymAddr: "00000000",
  trainingActionId: "123",
  deviceId: "3"
}]

for (let i = 0; i < arr.length; iPP) {
  wx.request({
    url: "https://localhost:8080/trainingRecord/single",
    data: arr[i],
    method: 'POST',
    header: {
      'content-type': 'application/x-www-form-urlencoded'
    },
    success: function(res) {
      console.log('res', res);
      console.log(this)
      if(res.status == 200){
          console.log(i)
      }
    }
  })
}

for (let i = 0; i < arr.length; iPP) { // 
  request({
    url: "https://localhost:8080/trainingRecord/single",
    data: arr[i],
    method: 'POST',
    header: {
      'content-type': 'application/x-www-form-urlencoded'
    },
    success: function(res) {
      console.log('res', res, i);
      console.log(this)
    }
  })
}

or:

for (var i = 0; i < arr.length; iPP) {
 // 
  (function(i){
    request({
        url: "https://localhost:8080/trainingRecord/single",
        data: arr[i],
        method: 'POST',
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: function(res) {
          console.log('res', res, i);
          console.log(this)
        }
    })
  })(i)
}

closure or use the index in forEach as the index

Menu