0
function getRequest(url)
{
    return $.ajax({
        url: url,
        type: "GET",
    })
}

function ABC()
{
    let url = "simple/url";
    let response = GET.getRequest(url);
    return response;
}

let global_var = "";

$.when(ABC()).then(response => 
{
    global_var = response;
    console.log(global_var); // -> it will show response
});


$(document).ready(function()
{    
    console.log(global_var); // -> empty string
});

Is there a way to pass response to global_var ? Code in document.ready function is executing before ajax calls, so thats why global_var is empty string but is there any other way to deal with it ? I can of course move whole document. ready function to $_when() but I want to avoid it becasue code will be less readable. How to wait for global_var assigment by response ?

  • `$.when()` is pointless here. – Quentin Mar 10 '22 at 14:21
  • If you want to use a value from a promise at some arbitrary point in time, **store the promise** in a variable. Don't try to store its resolved value. Use `then` on the variable when you need it. – Quentin Mar 10 '22 at 14:21

0 Answers0