0

I am attempting to access color options from a chrome extension, when I run this code, the second output is undefined, even though the variable is supposed to be able to be accessed by every part of the code because I defined it in the beginning.

var color;

function updateColor(){
  chrome.storage.sync.get("favoriteColor", function(result) {
    color = result.favoriteColor;
    console.log(color);
  });
  console.log(color);
};

updateColor();

I have searched all over the internet and I am unable to come up with a solution, any help would be greatly appreciated :)

gecko
  • 26
  • 3
  • pay attention to the order in which the `console.log()`s are called, and you'll (hopefully) understand why you have to handle asynchronous callbacks differently. – Patrick Roberts May 01 '18 at 13:43
  • 1
    it might be global, but it's value will only be filled after the callback is called. – Icepickle May 01 '18 at 13:46
  • That is just the function, even when I run the it, it still doesn't give color a value. – gecko May 01 '18 at 13:48

0 Answers0