2

I have following code to store some string value in storage:

function setData(dat) {
chrome.storage.sync.set({'key': dat}, function() {});
 }

function getData() {
chrome.storage.sync.get('key', function (obj) {
    alert('Your code is: ' + obj);
  });
}

And following usage of this code in my content script:

alert('processing');
setData('someVal');
getData();

But "getData" function always returns "object Object" instead of "someVal". What exactly I am doing wrong?

turing
  • 137
  • 1
  • 7
  • 2
    chrome API is [asynchronous](https://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call) so you need to get the value in `set` callback. – wOxxOm Oct 27 '17 at 12:53
  • 1
    Apart from the asynchronous tip from @wOxxOm, change `alert ('Your code is: ' + obj)` by `alert ('Your code is: ' + obj.key)`. – Iván Nokonoko Oct 27 '17 at 13:06
  • @IvánNokonoko , thank's It works for me – turing Oct 27 '17 at 14:28
  • Related/duplicates: [Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference](https://stackoverflow.com/q/23667086) and [How to return the response from an asynchronous call?](https://stackoverflow.com/q/14220321) – Makyen Oct 28 '17 at 01:21

0 Answers0