0

I have this really simple function, which tries to lookup a value in the local storage:

function get_sectoken() {
    var sectoken;
    chrome.storage.local.get(null, function(obj) {
        if (obj.sectoken_val) {
            sectoken = obj.sectoken_val;
            console.log("Security token: " + sectoken);
        } else {
            console.log("Security token not found!");
        }
    });
    return sectoken;
}

If I try to assign it to a variable, I can see that console.log() has fired up:

st = get_sectoken();
Security token: UoMjWVeEeEqxeCKpRYaMmxIGAFyofEpC

But if I check what has been assigned to this variable, I see it's undefined:

st
undefined

What am I doing wrong?

NarūnasK
  • 4,564
  • 8
  • 50
  • 76
  • You can't make an asynchronous function synchronous. – Felix Kling Aug 20 '15 at 13:54
  • OK, how can I improve my function and make it return values from the `storage.local`? Since values are stored locally (to my understanding on the machine, not synced to gdrive) asynchronous function is the last thing I need. – NarūnasK Aug 20 '15 at 14:00
  • Doesn't look like Chrome provides a synchronous API. So the answer is: you can't. – Felix Kling Aug 20 '15 at 14:56

0 Answers0