0

I have this function:

function getLink() {
        if (linkTypes.length == 0) {
          $.ajax({
            type: "GET",
            url: "/theServiceToCall.html",
            dataType: "json",
            contentType: "application/json; charset=utf-8",
          })
            .done((response) => {
              linkTypes = JSON.parse(response.d);
            })
            .fail((error) => {
              console.log(error?.responseJSON?.Message);
            });
        }
            return linkTypes;
      }

The problem is that it is getting inside the "if" so it do the ajax and then return the value and in the end doing of the page load the .done AKA:

linkTypes = JSON.parse(response.d);

So it return the wrong value of the table at first time. I need it to do that in first load the ajax is running after that save it into "linkTypes" and at the very end it will be returned. What should I do?

I tried to see why it do it like that but didnt get any solution.

Try_hard
  • 5
  • 4
  • 1
    Does this answer your question? [Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference](https://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron) – Ivar Mar 21 '23 at 13:37
  • I tried unfortunately it didn't work. – Try_hard Mar 22 '23 at 06:10

0 Answers0