Executing synchronous queries to Google Cloud Endpoints in Javascript

97 views
Skip to first unread message

Juan Antonio Fernández Sánchez

unread,
Apr 13, 2016, 4:23:34 AM4/13/16
to Google App Engine
I have found a problem with GAE Cloud Endpoints when I  try calling a function and I need the result of this function to other call.
I know that I could resolve this problem to other way, but I want understand how I can do this.
This is the summary, working in JavaScript.
I call  to a gapi function to send an image to Server, when the process to save finish, it return an url and i use this url to save the complete data to user.

The problem is that the users data always is saved before than the response arrive and it is saved without url that I need because the second call don't wait to first call.


I have tried to use callbacks and  other tools but I don't find the way.

Somebody had been the same problem? Thanks you.

Nicholas (Google Cloud Support)

unread,
Apr 13, 2016, 3:52:32 PM4/13/16
to Google App Engine
Thanks for posting your questions here! You mentioned that this is something you are trying to accomplish in Javascript. If I understand your task correctly, you would like to perform the following:
  • Call gAPI function to send image to server
  • When gAPI call returns a URL (likely where the image can be found), provide URL to the user
  • Save the complete data to the user
To help with this, it would be helpful if you could clarify a few things:
  • Where is the Javascript executing? (in browser using XMLHttpRequest or Nodejs using the http package)
  • Is the provided URL required for the complete save?
  • Where is each part of this code executing (client device / App Engine servers)?
Knowing the above should help us get more specific information to determine the best course of action for you.

Juan Antonio Fernández Sánchez

unread,
Apr 15, 2016, 4:12:44 AM4/15/16
to Google App Engine
Thanks you so much for your answer, is very useful have a people like you that help us.

Well, I have an angularjs app. Inside it I have a javascript code that call my gAPI (endpoint). The code is something like this:

function uploadImage(nombre, raw){
     var urlImage;
gapi.client.helloworld.imagenes.uploadImage({'name':nombre, 'image':raw}).execute(function(resp) {
urlImage=resp.message;
});
return urlImage;
};

And after i have other call to api to send complete data of user with the image url.
I know that I could have a function that loading all data of user (included the photo profile) but I have the code as well .Finally I've got the goal, calling the second call inside of first and it works right, but I would like know how make this correctly.
Thanks for all and sorry for my dreadful english.

Nicholas (Google Cloud Support)

unread,
Apr 15, 2016, 3:31:15 PM4/15/16
to Google App Engine
From the code sample and response provided, I am assuming the following:
  • The javascript is running on the client side
  • gapi calls are essentially asynchronous XMLHttpRequests
  • .execute() methods take a callback function that will be invoked when the API call returns success or failure
I believe the issue lies with how you may be handling asynchronicity. gapi calls are made asynchronously so that other javascript on the client can continue executing while the network request is made. For instance In the following example, the second console.log() will most likely be invoked first as the api call make take time to return.
api.execute(function () {
  console.log('API response');
});
console.log('Second invocation');

If the second console.log() must be invoked after the first console.log(), it must be placed within the callback function like so:
api.execute(function () {
  console.log('API response');
  console.log('Second invocation');
});

With this in mind, if you must have a few gapi calls happen sequentially, the calls must be made from within each callback. For example, to sequentially execute authenticate, uploadImage, saveUser, this would have to be written somewhat like this:
authenticate(currentUser).execute(function () {
  // authenticatation is done, proceed
  uploadImage(imageToSend).execute(function () {
    // image was uploaded
    saveUser(currentUser).execute(function () {
      // user was saved
    });
  });
});

I hope this example is helpful for understanding the logic of asynchronous APIs and callbacks. For many gapi libraries, you may want to look at using Promises to make this sort of logic somewhat clearer.

On Wednesday, April 13, 2016 at 4:23:34 AM UTC-4, Juan Antonio Fernández Sánchez wrote:

Juan Antonio Fernández Sánchez

unread,
Apr 16, 2016, 5:34:32 PM4/16/16
to Google App Engine
Thanks you so much for the explication Nicholas.

I've done exactly the same that you propose, in spite of I don't understood that I was doing exactly.
Thanks you so much for your explanation.
Reply all
Reply to author
Forward
0 new messages