0

I have a function that if returned true displays a message and if returned false it doesn't display it.

So i am trying to with the help of ajax check my database if the user should have the message displayed or not. This is what i got right now that is not working, the response goes into the if reponse == true but the return does nothing.

exports.show_message = function callback(response){

  if (response == true) return true;
  else if(response == false) return false;

    $.ajax({
      type: 'GET',
      url: "/account/show_msg/",
        success: function(data) {
          if (data) data = false;
          else data = true;
          callback(data);
        },
    }); 
 };

As mentioned it goes into response true but the return true; does nothing. However it works to just call the return true and ignore all checks

exports.show_message = function callback(response){

  return true;

    };
  • You're calling `return` before the AJAX request is even made, so the function will exit. The logic of the `if` statement is also needlessly complicated; just do `return response`, just not in the place you currently have it – Rory McCrossan Jun 22 '18 at 09:57
  • @RoryMcCrossan - Well, maybe. There are lots of values you can pass as `response` that won't match either of those conditions. `undefined`, any non-blank string, a number greater than 1... – T.J. Crowder Jun 22 '18 at 09:58
  • leffe - What does your initial call to `show_message` look like? In particular, what are you passing as `response`? – T.J. Crowder Jun 22 '18 at 09:58
  • @T.J.Crowder true, although that would seem at odds with the point of the code. It seems we need some clarification from the OP – Rory McCrossan Jun 22 '18 at 10:00
  • This looks like an attempt to make `show_message` *return* the result of the calculation on `data` in the `success` handler. It can't. `show_message` can't *return* a value based on the ajax call at all (without making the call synchronous, which is a very bad idea). See the linked question's answers for what to do instead. – T.J. Crowder Jun 22 '18 at 10:01
  • @T.J.Crowder I am passing either a timestamp value or an empty response so either data has something in it or its empty – leffe petttson Jun 22 '18 at 10:05
  • @RoryMcCrossan The ajax request is running since none of the if statements gets triggered from the start, i am trying to set a return statement based on what response i get from the ajax request. So in this case i am either going to get an empty value or a value with something in it and based on what i get i want to return true or false. – leffe petttson Jun 22 '18 at 10:08
  • @T.J.Crowder I have read the link you set as duplicate before posting this but im unsure of what to do in my situation. You are correct in your idea of what i am trying to do. And yes i dont want to make the call synchronous i could just set async to false but that is as you say a very bad idea. Could you provide some ideas of what i should try and do to make this work in my situation ? – leffe petttson Jun 22 '18 at 10:10
  • @leffepetttson - I can't provide any ideas that aren't covered by the answers to that question: Have `show_response` accept a callback it calls with the result, or return a promise. Either way, the code calling it will have to handle the fact it provides its information asynchronously. – T.J. Crowder Jun 22 '18 at 10:11
  • @T.J.Crowder Alright thank you, i have tried a lot of different varieties, let me try and explain what problem im facing and perhaps you could give some input on what i could try. Right now when a page loads a function is called, this function should return true or false, depending on what to return comes from the value i get back from my ajax request. I have tried with callbacks but that doesnt seem to work since it calls for my response on page load. Any ideas are helpful thank you – leffe petttson Jun 22 '18 at 15:27
  • @leffepetttson - As I said above (and as the answers to that question say): You can't do that. The ajax call is asynchronous. With a normal function, you cannot return the result of an asynchronous operation, because when the function returns, the process is not yet complete. So, again, use callbacks. The code using `show_message` **cannot** just branch on its return value. Instead, it needs to pass in a callback and, when the callback is called, *then* branch based on the result. – T.J. Crowder Jun 22 '18 at 15:29

0 Answers0