Javascript function call asynchronous: Calling function call parameters in callback function

How can I get the user state in a Javascript callback function? How I have a Javascript funciton making an async call like this. Now, in the callback function, I need to access the user state. How can i do this? In Silverlight, we have a custom view of things. Do we have the same mechanism in Javascript. Please, help.

Note. I don't want to use a global variable as Func1 () will execute in a For loop.

   function Func1() {
       var userState = "someValue";
       geocoder.asyncCall(parameters , CallBack);
   }


   function CallBack(result) {

       // Use result
       // How to access userState in this function
   }

      

+3


source to share


2 answers


Try this code:

function Func1() {
   var userState = "someValue";
   geocoder.asyncCall(parameters ,function(){ 
      CallBack(userState);
   });
}


function CallBack(result) {

   // Use result
   // How to access userState in this function
}

      



Update

function PlotAddressOnMap(address) { 
   var address = address; 
   var userState="userState";
   geocoder.geocode({ 'address': address }, CityDetailsReceived(userState)); 
} 

function CityDetailsReceived(userState) {
   return function(results, status){
      //your code
   }
} 

      

+5


source


function Func1() {
   var userState = "someValue";
   geocoder.asyncCall(parameters , CallBack(userState));
}


function CallBack(userState) {
   return function(result){
       // userState is accessible
   }
}

      



+4


source







All Articles