Since in this case the getValue function getValue nothing, the result of the execution
getValue(variable, defaultvalue)
will always be undefined .
Possible solutions:
Forward callback - a function that will be called upon successful execution, for example
function getValue(variable, defaultvalue, successCallback) { chrome.storage.sync.get({ [variable]: defaultvalue, }, function(items) { if (items[variable]) { successCallback(items[variable]);//вызываем callback если все хорошо } }); }
And call it as follows:
getValue(variable, defaultvalue, function(items){//items внутри этой функции - будут нужным значением ... })
You can use the Promise , like this:
function getValue(variable, defaultvalue, successCallback) { return new Promise(function(resolve, reject){ chrome.storage.sync.get({ [variable]: defaultvalue, }, function(items) { if (items[variable]) { resolve(items[variable]);//говорим что все хорошо }else{ reject(/*тут можно указать причину почему все плохо*/); } }); }); }
And use so
getValue(variable, defaultvalue).then(function success(items){//items внутри этой функции - будут нужным значением ... });