javascript - Chrome.storage.sync.get not storing value in local variable -
function getbugval() { var bugval = ""; chrome.storage.sync.get('bugid', function (obj) { console.log(obj.bugid); bugval = obj.bugid; console.log(bugval + "<- val inside sync"); }); console.log(bugval + "<- val outside sync"); return bugval; }
if call getbugval() return value keeps indicating empty string instead of actual value chrome.storage.sync.get. bugval not saving string value.
console.log(bugval + "<- val inside sync");
yields correct value within inner function call. thoughts?
yep. that's how async code works. you'll have use callbacks. work.
function workwithbugval(val) { // stuff } function getbugval(callback) { var bugval = ""; chrome.storage.sync.get('bugid', function (obj) { console.log(obj.bugid); bugval = obj.bugid; callback(bugval); }); } getbugval(workwithbugval);
Comments
Post a Comment