1

I am trying do an asynchronous call in a loop, use the returned result and also make the variable 'q' available to the inner function.

How would I make q available to to the inner function (with the same value as it was before async call)?

var oController = this;
for (var q = 0; q < dataArray.length; q++) {
  var InspectionNo = dataArray[q].inspectionNo;

  //async call
  oController._validateInspection(InspectionNo)

    //returns flag
    .then(function(flag) {
      debugger;
      if (flag) {
        //q is not defined here
        oController._doSomething(q)
      }
    });
}
neeko
  • 1,930
  • 8
  • 44
  • 67

1 Answers1

1

try something like this:

var oController = this;
for (var q = 0; q < dataArray.length; q++) {
            var InspectionNo = dataArray[q].inspectionNo;

            //async call
            Promise.all([oController._validateInspection(InspectionNo), q])

            //returns flag
            .then(function([flag, q]){
                    if(flag){
                        //q is not defined here
                        oController._doSomething(q)
                    }
                });
        }
kharandziuk
  • 12,020
  • 17
  • 63
  • 121