function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
TehNrdTehNrd 

Javascript Remoting and order of execution

I have the following method that is performing a javascript remoting call:

 

function doRemote(){
	alert(1);
	
	MyClassName.searchUsers('test',function(result,event){
		//Process the result
		alert('2');
	});
	
	//return the result
	alert(3);
}

 

What I thought would happen is the alerts would execute in 1, 2, 3 order. What actually happens is 1, 3, 2. Is there anyway to control this order of execution? Ideally would like to be able to call a method that performs a JS remoting call, process the results, and then return the results. Seems like a simple request but I can't seem to accomplish this.

 

Thanks,

Jason

 

sherodsherod

No, the call is asynchronous

 

What's happening is you are calling alert ('1'), registering alert('2');  in the callback response, and then calling alert 3.  Later, when the response is returned from the server, alert 2 is executed.

 

You can't make an asynchronous call synchronous, you need to put the logic you want to continue with inside the callback function (where you have alert('2') now) (if not there, call it from there).

 

 

 

 

TehNrdTehNrd

Hmm...for some reason I thought javasript callouts where synchrounous. In the sense that all code would stop executing and wait for the reponse of the callout and then it will resume when the call out completes....but I guess not.

 

For those wondering I was hopeing to use the call back option in the source option of jQuery autocomplete widget but it seems like this isn't going to work as expected.

TehNrdTehNrd

Figured it out. This seems to work with jQuery autocomplete widget:

 

j$("#ownerSearchField").autocomplete({
    source: function(request,response){
                var userValues = [];
                ChangeLeadOwner.searchUsers(request.term,function(result,event){
                    if(event.status == true){
                        for(var i = 0; i < result.length; i++){
                            userValues[i] = result[i].Name;
                        }
                        response(userValues);
                    }else{
                        alert('Uh oh. Something broke. Reload the page and try again');
                    }
                });   
            }
});

 

cwall_sfdccwall_sfdc

Visualforce remoting invokes calls synchronously, but it's response is asynchronous.  This is typical AJAX.

 

You can handle the response directly in the remoting callback function or the remoting callback function can pass the result to another function.  It looks like your example is a little of both.

 

 

gene.koopmangene.koopman

Jason, if you're interested in AutoComplete, you should check out this code: Salesforce Javascript Remoting, jQuery and Autocomplete.

 

-Gene

Ankit AroraAnkit Arora

A small question from my side (bit off the the topic) : If Javascript remoting is an asynchronous call then what is the difference between Javascript Remoting and WebService?

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

cwall_sfdccwall_sfdc

Few things come to mind, but really depends on what you're doing.

 

- VF remoting is simple.

- VF remoting is well-integrated with VF pags/components and Javascript.

- VF remoting is lighter in terms of payload.

- VF remoting has broader parameter and return type support.

- No limit considerations (currently).

- In Summer '11, VF remoting allows for public remoted methods (webService require global).