While trying out Google's Javascript API with Google+, I ran across a snag.
var response;
var request = gapi.client.request({
'path': '/plus/v1/people/' + "THEUSERID",
'params': {}});
request.execute(function(resp){});
The execute function (gapi.client.HttpRequest.execute) takes a single argument; a callback function. However, I do not wish to handle the data immediately after I receive it, I want to store it in the response
variable I declared at the start of the code. Is there a way to use the callback argument to store the response?
Forgive me if this has an obvious solution, I'm a little new to JavaScript.
EDIT: It has been suggested that the callback function be as follows:
request.execute(function(resp){response = resp;});
However, something curious happens with the function. Here is the code I used to test:
var response;
var request = gapi.client.request({
'path': '/plus/v1/people/' + userID,
'params': {}});
request.execute(function(resp){
console.log("RESP:");
console.log(resp);
response = resp;});
console.log("RESPONSE:");
console.log(response);
What the console outputs is as follows:
RESPONSE:
undefined
GET https://www.googleapis.com/plus/v1/people/104815258973759324455?key=XXXXXXX
RESP:
({theactualjsondatathatIreceivedfromthecall})
Apparently, the code continues executing /before/ the execute callback function can be called. I need a way to check for this, so that the code after the execute function is not called until the callback function is run.