title: Returning value of a nested function in NodeJS
body: <p>I am trying to return value from function inside function in a nodejs app. Its always coming out to be undefined.</p>
  
  <pre><code>var geo = {
      list: function(callback){
          var returnval;
          gapi.getcodes(function(error, data) {
              if (error) {
                 console.log(error);
               } else {
                 returnval = data; 
                 console.log(returnval);
              }                        
          });
          callback(returnval);                
        }            
  }
  
  var geocodes = geo.list(function(){});
  console.log("Value of geocodes: "+geocodes);
  </code></pre>
  
  <p>I know gapi.getcodes is asynchronous function and i read on another <a href="https://stackoverflow.com/questions/5596026/returning-value-from-function-inside-a-function">thread</a> that I should be passing callback function to get the return value. But still the value of geocodes is still coming out to be undefined.</p>
  
  <p>When the code runs i see the value returned in </p>
  
  <blockquote>
    <p>console.log(returnval)</p>
  </blockquote>
  
  <p>. 
  How do I get this value to be stored in a var?</p>
  
  <p>I am not sure what is wrong. Please help I am trying to learn javascript and object style pattern. </p>