I’m reading Chrome’s extension localStorage in my content script. If I log the variable to console, everything works well. If I want to alert it, it says variable is undefined.
var data666;
chrome.extension.sendRequest({method: "getLocalStorage", key: "autoplay"}, function(response) {
console.log(response.data); // works perfectly
data666 = response.data;
});
alert(data666); // does not work
EDIT: I need to use the variable (data666 in this case) outside the asynchronous function.
note that
sendRequestis an asynchronous function, data’s value will not be set untilsendRequestexecutes the callback function.Therefore, you need to move the alert statement into the callback function.