-1

I have a function that finds the name of the user from the database, according to the input userID. But I am facing difficulty in returning the name.

function getName(userID){
     var user = { "username" : userID};
     var name;
     $.get("/getName",user,function(data,status){
         console.log(data);  //prints correct result
         return data;        //but returns undefined
     });
}

The returned value is always undefined. The calling function looks like this :

function init(){
     myid = "james123";
     myname = getName(myid);
     console.log(myname);     //always prints undefined
}

If I try to save the data received from the database in a variable, and return it later, like :

function getName(userID){
      var user = { "username" : userID};
      var name;
      $.get("/getName",user,function(data,status){
          console.log(data);
          name = data;
      });
      return name;
  }

This also doesn't work and I know because it is asynchronous. Any way to make this return work?

Pushpendu Ghosh
  • 105
  • 1
  • 2
  • 7
  • return `$.get` as well: `return $.get(...)`. You will get a promise, i guess, you can handle it inside `then` and `catch` blocks. – P.S. Jul 09 '18 at 16:17

2 Answers2

1

The $.get is executed asynchronously so it needs a callback. You can do it like this:

function getName(userID, callback){
     var user = { "username" : userID};
     var name;
     $.get("/getName",user,function(data,status){
         console.log(data);  //prints correct result
         callback(data);        //but returns undefined
     });
}

and then:

function init(){
     myid = "james123";
     getName(myid, function(myname){
         console.log(myname); 
     });
}

Or with Promises you can:

function getName(userID){
     var user = { "username" : userID};
     var name;
     return new Promise(function(resolve, reject) {
         $.get("/getName",user,function(data,status){
             resolve(data);  //prints correct result
         });
     }
}

and then:

function init(){
     myid = "james123";
     getName(myid).then((myname)=>console.log(myname);
}

and with async:

async function init(){
     myid = "james123";
     myname = await getName(myid);
     console.log(myname);
}
Giannis Mp
  • 1,291
  • 6
  • 13
0

You can used the lower level ajax method as this allows you to customise more of the settings for the call than the higher level get does

One of the settings lets you disable the async call i.e. force your code to stop and wait for the get to complete before continuing this looks like async: false and is used like this:

function getName(userId) {
   var user = { "username" : userID};
   var name;
   $.ajax({
      url: "/getName",
      data: user,
      type: "GET",
      async: false,
      success: function (data) {
          name = data;
      },
   });
   return name;
}
MikeT
  • 5,398
  • 3
  • 27
  • 43
AitorFDK
  • 101
  • 6
  • 1
    your answer is a little confused as you aren't explaining that the ajax function allows for greater configuration of the command or that the only thing actually important there is the `async: false` – MikeT Jul 09 '18 at 16:36
  • Thanks for the edit @MikeT, I need more practice answering on StackOverflow. – AitorFDK Jul 09 '18 at 16:42