Search code examples
javascriptjqueryhtmllocalforage

jQuery global variable access confusion


I have very specific problem, I am attempting to get the data using localforage library, assigning it to a global variable, which is a JSON.

$.unify.data.assets = {};
$.unify.initialization.private.intializeDefaultLayout = function() {
    localforage.getItem('assets', function($value) {
        $.unify.data.assets = $value;
        console.log($.unify.data.assets); // This shows all object in the record

    });
    console.log($.unify.data.assets); // This is empty, even on global variable assignment
};

I am very confused in this problem, kindly correct the required


Solution

  • Because this line is being executed in a callback

    $.unify.data.assets = $value;
    

    So there are chances the following line has been called before callback executed

    console.log($.unify.data.assets); // This is empty, even on global variable
    

    If so the $.unify.data.assets is not changed when you did a console.log so it's blank.