Does Chrome.storage.sync.get synchronize with an external level object, but the internal object is not synchronized?

I am trying to use chrome.storage.sync.getto return the settings. I do not understand that when I call console.log(settings), it returns the correct values. But if I call console.log(settings.speeds), it will return the old values. I think this has something to do with the asynchronous nature chrome.storage.sync.get. Can someone explain what is going on here? And if there is a solution to this. I tried to use callback, but it did not help. I assume that one solution is to use only one level, but that is not what I want.

Thank you all for your help.

var settings = {
    speeds: {
        speedInput1: 1.0,  // after get, new value should be 11.23
        speedInput2: 2.0   // after get, new value should be 4.50
    },
    shortcuts: {
        shortCut1: '1',
        shortCut2: '2'
    }
};


chrome.storage.sync.get(settings, function(result) {
    // Retrieve speed settings
    for (var key in settings.speeds) {
        if (key in result.speeds) {
            settings.speeds[key] = result.speeds[key];
        }
    };

    // Retrieve shortcut settings
    for (var key in settings.shortcuts) {
        if (key in result.shortcuts) {
            settings.shortcuts[key] = result.shortcuts[key]
        }
    };
});

console.log(settings); // correct updated values
console.log(settings.speeds); // old values
+4
source share
1 answer

, . get , . , ... . .

function getChromeStorage() {
    chrome.storage.sync.get(settings, function(storage) {
    // get stored values back;
}
getChromeStorage(); // calling it as a function solves the asynchronous issue
+1

All Articles