function load(data_field) { var response_object = {}; //объект в который нужно вернуть значение из коллбека chrome.storage.sync.get(data_field, function (result) { response_object = result; // сохраняем в объект результат }); return response_object; // доожен содержать объект, хотябы с одним полем } 

Why response_object does not change ??????

Reported as a duplicate at Grundy. javascript Jan 3 '17 at 12:39 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • chrome.storage.sync.get asynchronous? If so, then your return response_object work before the response_object gets the value - br3t

2 answers 2

Try the callback option

 function load(data_field, callback) { var response_object = {}; //объект в который нужно вернуть значение из коллбека chrome.storage.sync.get(data_field, function (result) { response_object = result; // сохраняем в объект результат callback(response_object); }); } 
  • Yes, it began to work with a callback, but only in the console. If you use this function in the context of an extension, then an empty string is returned - Alexander
  • @ Alexander, look for a mistake in logic. You can test this function by running it immediately after writing the read variable. - br3t

Maybe something with result ?

Try return result or even

 if(typeof result === 'object') { response_object = result; // сохраняем в объект результат return response_object; } else { alert('Error - typeof result = ' + typeof result + ' value = ' + result); } 

If result is a text or number according to the plan, then in order to assign a parameter to the object, use Object.result = result; return Object Object.result = result; return Object

  • C result everything is fine, because it outputs the normal result to the console inside the callback - Alexander