0

I know this is a question that's been asked to death, but I still struggle with how to use callbacks. I just want to return a value from the ajax's request.done outcome. I tried to follow Felix Kling's very informative answer on using callbacks with AJAX, but can't get my code to work.

My code below submits a form with user input data to the database. I would like it to retrieve the property employeeId. I am able to alert the correct value inside the submit function, but I would like to use this value in other functions. From reading up on asynchronous functions,i think this is because submit function exits before returning the value. I would really appreciate some guidance on using callbacks to return values.

I tried to only include the code pertaining to my issue

$('#employeeForm').submit(function(event,callback){
  var values = $(this).serialize(); //gather form data
  var result = foo(); //callback implementation from Felix Kling's SO answer
  foo(function(result){
        var request = $.ajax({
        url: "game.php",
        type:"post",
        dataType:"json",
        data: values
    });

  function foo(response.employeeId){ //I am trying to return value response.employeeIda  in the success situation
    request.done(function (response,textStatus,jqXHR){
        document.getElementById("submitEmployeeInfo").innerHTML=response.employeeId;    
        var employeeId = response.employeeId;

    }
  });
})
Phil
  • 157,677
  • 23
  • 242
  • 245
I Like
  • 1,711
  • 2
  • 27
  • 52
  • This doesn't look syntactically valid at all. I suspect your console is full of errors – Phil Aug 07 '17 at 00:12
  • unfortunately my console doesn't log anything. yes, i had no idea how to adapt callback code for my ajax function :( – I Like Aug 07 '17 at 00:31
  • 1
    If I copy and paste your code into my JS console, I immediately get *"Uncaught SyntaxError: Unexpected token `.`"*. Either you're looking in the wrong place or the code in your question is not the code you're running – Phil Aug 07 '17 at 00:40
  • FYI, it's due to `function foo(response.employeeId)`. You cannot use a dot property in a function argument – Phil Aug 07 '17 at 00:41
  • 1
    "I just want to return a value from the ajax's `request.done` outcome" You can't. "I am able to alert the correct value inside the submit function, but I would like to use this value in other functions" Then call those functions from inside the `done` function. You can't return a value from an asynchronous function. (Actually with ES8 you can, using `async`/`await`, but you should first learn callbacks, then promises, only then are you ready to deal with `async`/`await`.) – Amadan Aug 07 '17 at 01:02
  • When dealing with asynchronous code, your first question needs to be "and what do I want to do with this value later?" You can't cheat and say "return it" - it's the same level of douchery (although with JS beginners it's usually unintended) like when people say "yes I do" when you ask them if they know what time it is. "I want to display it" - display it from the callback. "I want to run this other function with it" - run this other function from the callback. "I want to calculate something and store it at serverside" - then do so... from the callback. – Amadan Aug 07 '17 at 01:05

0 Answers0