You need to sign in to do that
Don't have an account?
TehNrd
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
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).
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.
Figured it out. This seems to work with jQuery autocomplete widget:
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.
Jason, if you're interested in AutoComplete, you should check out this code: Salesforce Javascript Remoting, jQuery and Autocomplete.
-Gene
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
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).