0

I'm trying to pass my OData.read result into a global and a local variable, but eventually, each of them is undefined.

var foo_global;

function doIt()
{
    var foo_local;

    $.ajaxSetup
    ({
        cache:false
    });

    OData.defaultHttpClient.enableJsonpCallback = false;

    OData.read(url,
        function (data)
        {
            foo_local = data;
            foo_global = data;
            alert(data); // works!
        },
        function (err)
        {
            alert('error');
        }
    );

    alert(foo_local); // undefined
    alert(foo_global); // also undefined
}
user3601578
  • 1,310
  • 1
  • 18
  • 29
  • Your problem is that `read` is asynchronous, and you're alerting the variables before they will get assigned. – Bergi Jan 22 '15 at 17:22
  • Ok thank you. How can I check if the function is ready? Unfortunately, the DataJS-documentation does not tell me anything helpful. – user3601578 Jan 23 '15 at 11:41
  • It's calling your callback when it's ready! Put everything that needs to wait for the result in the callback function. – Bergi Jan 23 '15 at 11:45
  • Ok thanks. Looks like there is no other way, since I tried to avoid putting everything in the callback function. – user3601578 Jan 23 '15 at 12:45

0 Answers0