0

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

Kevin King
  • 557
  • 1
  • 9
  • 25
  • 4
    Possible duplicate of [Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference](http://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron) – Haibara Ai Jun 30 '16 at 01:16
  • 1
    Similar questions have been asked many times :), take a look at above link, about asynchronous call – Haibara Ai Jun 30 '16 at 01:16

1 Answers1

2

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.