+ Start a Discussion
SFDC-NOOBSFDC-NOOB 

Is it possible to execute remote actions in order?

I am using javascript remoting.  I have a situation where I need remote actions to execute in order.

The problem I am running into is the second remote action executes prior to the first one finishing.  Seems like I've tried everything!  Since remote actions are asynchronous, is it possible to execute them in order?  What is the best way to do so?


    Visualforce.remoting.Manager.invokeAction(
      '{!$RemoteAction.ChoptankRates.method1}',
      document.getElementById('var1').value,
                   
      function(result, event) {
        document.getElementById('var1').value = result[0].somevalue;
       
        //Execute second remote action now.
      }
    );
    Visualforce.remoting.Manager.invokeAction(
      '{!$RemoteAction.ChoptankRates.method2}',
      document.getElementById('VAR1').value,

      function(result, event) {
        //do something here
      }
    );
Best Answer chosen by SFDC-NOOB
pconpcon
You'll either need to call the second one from inside the call back method, or look at using something like q / javascript promises [1] to handle the order of operations.

[1] https://github.com/kriskowal/q

All Answers

pconpcon
You'll either need to call the second one from inside the call back method, or look at using something like q / javascript promises [1] to handle the order of operations.

[1] https://github.com/kriskowal/q
This was selected as the best answer
SFDC-NOOBSFDC-NOOB
Turns out it was much simpler than this.  A response from a callout contained a null value.  The second remote action executed and referenced the null value giving the illusion that the second method was executing prior to the first.  I put conditions in place to handle the null value.