1

I'm trying to update a local variable in a function with the returned data from a callback function. But it's looking like the callback function doesn't have access to it.

Below is what I'm working with:

this.renderUI = function(res) {
    var connected = (res.user != null && res.user.isConnected);
    if(connected) {
      $j('#jive-modal-invite').trigger('close'); 
      var contactsData = gigya.socialize.getContacts({callback: getContacts_callback });
      console.log(contactsData);  
    }else {
      console.log('openid disconnected');
    }   
  };

  function getContacts_callback(response) {
    return response;
  }
5
  • The callback is asynchronous, so it does not know about the local variable. Commented Sep 11, 2013 at 8:55
  • Am I missing something here? Why is your callback like: gigya.socialize.getContacts({callback: function(response){ instead of gigya.socialize.getContacts(function(response){? Commented Sep 11, 2013 at 9:02
  • If getContacts is asynchronous, you simply cannot (and should not). Put the logging statements (and everything else the accesses contactsData) in the callback Commented Sep 11, 2013 at 11:20
  • @Bergi I can't access global variables from the callback either. Is this a known behaviour? Commented Sep 12, 2013 at 5:08
  • @drecute: Of course you can if they're really global. Commented Sep 12, 2013 at 9:59

1 Answer 1

1

console.log(contactsData); is being executed before your callback is complete try this:

  gigya.socialize.getContacts({callback: function(response){
    contactsData['contacts'] = response;
    console.log(contactsData); 
  } }); 
Sign up to request clarification or add additional context in comments.

2 Comments

I need contactsData outside of the callback scope
How about calling a function from inside your callback?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.