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,
speedInput2: 2.0
},
shortcuts: {
shortCut1: '1',
shortCut2: '2'
}
};
chrome.storage.sync.get(settings, function(result) {
for (var key in settings.speeds) {
if (key in result.speeds) {
settings.speeds[key] = result.speeds[key];
}
};
for (var key in settings.shortcuts) {
if (key in result.shortcuts) {
settings.shortcuts[key] = result.shortcuts[key]
}
};
});
console.log(settings);
console.log(settings.speeds);
source
share