0

I'm here because I want to get an arraylist from database but when i refresh my page the function is not instantly executed. I want to use a for loop on that array but it doesn't initialize the array before entering in the for loop.

Here is my function to request the array from database :

function fetch_constraint(){

    $.ajax({
        type:"GET",
        dataType: 'text',
        url: build_url(url_ttconstraints),
        async: true,
        contentType: "application/json",
        success: function(msg){
            constraints = JSON.parse(msg);
        },
        error: function (msg){
            console.log("error");
            show_loader(false);
        }
    })
    return constraints;
};

Then I call that function so the array can be in the head of the js file:

fetch_constraint();

as a result with console.log(fetch_constraint()); after the fetch function I get: undefined

But when i use the function in the console it works and I get the array.

Sorry for my bad english.

WAWABC
  • 5
  • 3

0 Answers0