Skip to main content
added 18 characters in body
Source Link
gNerb
  • 9.6k
  • 5
  • 27
  • 58

The issue I see with this is that your accessing the variable when the callback is called, not when the callback is written. Since this is asynchronous, the function you are defining as the callback is accessing the counter variable after all 5 instances of the action have been en-queued so counter would be 5 in all callbacks..

Passing extra data to a callback does not seem possible to me as you are not the one calling the method. Your method gets what ever parameters are passed to it by the action.setCallback method. In this instance, you get the response object.

My recommendation is move the for loop containing the counter to the apex side of the equationequation; if possible. RatherSend the list to the apex controller rather than looping through a list of data and callingen-queuing the methodaction 5 times, send the list to the method, have the method run. Have the controller loop, through the records and return a set of results that you can loop through in the javascriptJavaScript.

The issue I see with this is that your accessing the variable when the callback is called, not when the callback is written. Since this is asynchronous, the function you are defining as the callback is accessing the counter variable after all 5 instances of the action have been en-queued so counter would be 5 in all callbacks..

Passing extra data to a callback does not seem possible to me as you are not the one calling the method. Your method gets what ever parameters are passed to it by the action.setCallback method. In this instance, you get the response object.

My recommendation is move the for loop containing the counter to the apex side of the equation if possible. Rather than looping through a list of data and calling the method 5 times, send the list to the method, have the method run the loop, and return a set of results that you can loop through in the javascript.

The issue I see with this is that your accessing the variable when the callback is called, not when the callback is written. Since this is asynchronous, the function you are defining as the callback is accessing the counter variable after all 5 instances of the action have been en-queued so counter would be 5 in all callbacks..

Passing extra data to a callback does not seem possible to me as you are not the one calling the method. Your method gets what ever parameters are passed to it by the action.setCallback method. In this instance, you get the response object.

My recommendation is move the for loop containing the counter to the apex side of the equation; if possible. Send the list to the apex controller rather than looping through a list of data and en-queuing the action 5 times. Have the controller loop through the records and return a set of results that you can loop through in the JavaScript.

Post Undeleted by gNerb
deleted 98 characters in body
Source Link
gNerb
  • 9.6k
  • 5
  • 27
  • 58

I'm not a learned man and JavaScript andThe issue I have never been friends; howeversee with this is that your accessing the variable when the callback is called, my guessnot when the callback is written. Since this is asynchronous, the issuefunction you are defining as the callback is a resultaccessing the counter variable after all 5 instances of understanding scope and understanding asynchronousity (<-the action have been en- probably not even a word)queued so counter would be 5 in all callbacks..

I would try something likePassing extra data to a callback does not seem possible to me as you are not the one calling the method. Your method gets what ever parameters are passed to it by the action.setCallback method. In this: instance, you get the response object.

getTestList: function (cmp) {
    var action = cmp.get('c.getTestList');
    var counter
    for (counter = 0; counter <= 5; counter+=1) {
        action.setCallback(this, function (response) {
            var state = response.getState();
//--------------------------------------------
            var innerCounter = counter;
//--------------------------------------------
            if (state === 'SUCCESS') {
                // do something
            }
//--------------------------------------------
            console.log(innerCounter);
//--------------------------------------------
        });
        $A.enqueueAction(action);
    }
}

My recommendation is move the for loop containing the counter to the apex side of the equation if possible. Rather than looping through a list of data and calling the method 5 times, send the list to the method, have the method run the loop, and return a set of results that you can loop through in the javascript.

I'm not a learned man and JavaScript and I have never been friends; however, my guess is the issue is a result of understanding scope and understanding asynchronousity (<-- probably not even a word).

I would try something like this:

getTestList: function (cmp) {
    var action = cmp.get('c.getTestList');
    var counter
    for (counter = 0; counter <= 5; counter+=1) {
        action.setCallback(this, function (response) {
            var state = response.getState();
//--------------------------------------------
            var innerCounter = counter;
//--------------------------------------------
            if (state === 'SUCCESS') {
                // do something
            }
//--------------------------------------------
            console.log(innerCounter);
//--------------------------------------------
        });
        $A.enqueueAction(action);
    }
}

The issue I see with this is that your accessing the variable when the callback is called, not when the callback is written. Since this is asynchronous, the function you are defining as the callback is accessing the counter variable after all 5 instances of the action have been en-queued so counter would be 5 in all callbacks..

Passing extra data to a callback does not seem possible to me as you are not the one calling the method. Your method gets what ever parameters are passed to it by the action.setCallback method. In this instance, you get the response object.

My recommendation is move the for loop containing the counter to the apex side of the equation if possible. Rather than looping through a list of data and calling the method 5 times, send the list to the method, have the method run the loop, and return a set of results that you can loop through in the javascript.

Post Deleted by gNerb
Source Link
gNerb
  • 9.6k
  • 5
  • 27
  • 58

I'm not a learned man and JavaScript and I have never been friends; however, my guess is the issue is a result of understanding scope and understanding asynchronousity (<-- probably not even a word).

I would try something like this:

getTestList: function (cmp) {
    var action = cmp.get('c.getTestList');
    var counter
    for (counter = 0; counter <= 5; counter+=1) {
        action.setCallback(this, function (response) {
            var state = response.getState();
//--------------------------------------------
            var innerCounter = counter;
//--------------------------------------------
            if (state === 'SUCCESS') {
                // do something
            }
//--------------------------------------------
            console.log(innerCounter);
//--------------------------------------------
        });
        $A.enqueueAction(action);
    }
}