Kevin Kevin - 1 year ago 144
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 Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download