Kevin Kevin - 5 months ago 37
Javascript Question

google.chrome.storage undefined output

I got this weird output from chrome.storage

When I tried this code here

var local;
chrome.storage.local.get(function(data) {
local = data;
console.log(local);
});
console.log('>');
console.log(local);
console.log('<');


Here's the output (dataString is the value of data)

>
undefined
<
dataString


To which the dataString should be printed before the > undefined < right? or maybe I am just confused

Answer

As it says in chrome storage docs, chrome.storage is asynchronous, so the callback function from chrome.storage.local.get executes later and that is why you get that output.