0

Why does this variable (lanFound) become undefined?

I get the following output:

Lightbulb moment! :)

As I typed the sequence of output gives it away! ajax is asynch, so the true comes back after the code has continued! I'll post anyway, might be handy for someone!

  • testing for: DK
  • result is: undefined
  • /sites/cspdKnowledgeAssemblyPlatform/ApprovedContent/DKCover Letter.docx succeeded

I have a set of docx files, but am adding support for languages, but to test the files (docx) have been added I use the following code (OK this is a long had variant to allow me to debug):

    fileUrl = filePath + fileName;
        if (lan != "EN"){
            showNotification("testing for: " + lan);
            var lanFound = false;
                lanFound = checkURL(filePath + lan + fileName);
                showNotification("result is: " + lanFound);
            if(lanFound){
                debugger;
                fileUrl = filePath + lan + fileName;
                showNotification("found " + fileUrl); 
            }
        }

        function checkURL(urlFileName){
            $.get(urlFileName)
            .fail(function() {
                showNotification(urlFileName + " failed");
                return false;
            })
            .done (function() {
                 showNotification(urlFileName + " succeeded");
                return true;
            });
        }

You can ignore this - just added for context of "showNotification")

        function showNotification(content){
             var currentText =  $( "#resultpanel" ).html();
             currentText = currentText + "<br/>" + content;
             $( "#resultpanel" ).html(currentText);  
        }
Jerry Weeks
  • 317
  • 2
  • 13
  • and the question? – madalinivascu Nov 02 '16 at 10:03
  • Why does the variable become undefined! (see the output bullets) – Jerry Weeks Nov 02 '16 at 10:04
  • You are assigning a variable to a function execution return value: lanFound = checkURL(filePath + lan + fileName); If you go to checkURL function, you will see that this function doesn't event have a `return` statement, there is only a return inside the functions passed as callback for `.fail` and `.done` methods. Those callbacks WILL return a value, but again, checkURL is not returning something. Therefore, every function in Javascript that doesn't have a `return` statement, returns `undefined`. I donno why is it marked as duplicated. – Emilio Grisolía Nov 02 '16 at 10:09
  • Happy for this to be deleted if seen as a duplicate - as I explained, it was only as I typed I noticed where the fault comes from. Had I been awake, I would have fixed it! I have already resolved this type of issue several times in other places in my code! - callback works well enough! – Jerry Weeks Nov 02 '16 at 10:31

1 Answers1

1

You cannot call an ajax call in this manner, the code will pass by before the result is returned, due to it being asynchronous, the result being the variable is read as undefined at the point at which the code passes it.

Sorry as I typed the question, I realised the answer, but posted anyway, as this might be handy for someone.

Jerry Weeks
  • 317
  • 2
  • 13