title: returning values from an async delegate function
body: <p>I am working with the Sharepoint Client Object Model using javascript and looking to make as much of my code as reusable as possible.</p>
  
  <p>Examples of using the COM usually looks like this with a call and then a success delegate function to output the results.</p>
  
  <pre><code>function loadProfile()
  {
      var context = SP.ClientContext.get_current();         
      var web = context.get_web();         
      var userInfoList = web.get_siteUserInfoList();
      camlQuery = new SP.CamlQuery();         
      camlQuery.set_viewXml('');         
      this.listItems = userInfoList.getItems(camlQuery);         
      context.load(listItems);         
      context.executeQueryAsync(Function.createDelegate(this, this.onProfileSuccessMethod), Function.createDelegate(this, this.onFail));     
  
  }
  function onProfileSuccessMethod(sender, args) 
  {
      var item = listItems.itemAt(0);
      var first_name = item.get_item('FirstName');
      var last_name = item.get_item('LastName');
      alert(first_name + " " + last_name);
  }
  
  function onFail(sender, args)
  {
    alert('Error: ' + args.get_message() + '\n' + args.get_stackTrace());
  }
  </code></pre>
  
  <p>What I am trying to achieve is being able return the values after instantiating an object but given that it is asynchronous I am not sure how this is possible, would I need to "listen" for the call to complete before values are returned.</p>
  
  <p>Would it be something like this? Or can you not have a delegate function internal to the function it creates it from. Apologies for my limited understanding!</p>
  
  <pre><code>var profile = new loadProfile("testid");
  alert(profile.onProfileSuccessMethod());
  
  
  function loadProfile (id) 
  {
      this.user_id = id;
      var context = SP.ClientContext.get_current();         
      var web = context.get_web();         
      var userInfoList = web.get_siteUserInfoList();
      camlQuery = new SP.CamlQuery();         
      camlQuery.set_viewXml('');         
      this.listItems = userInfoList.getItems(camlQuery);         
      context.load(listItems);         
      context.executeQueryAsync(Function.createDelegate(this, this.onProfileSuccessMethod), Function.createDelegate(this, this.onFail));    
     this.onProfileSuccessMethod = function() 
  {
      var item = listItems.itemAt(0);
      this.first_name = item.get_item('FirstName');
      this.last_name = item.get_item('LastName');
          return this.first_name
  
  
      };
  }
  </code></pre>