I have been reading tons and tons but certain things I am just not seeming to be able to grasp my head around. Its almost as if chrome extensions break standard javascript.
So I am trying to call variables in one function that have been defined globally and set within another function. It should be standard js 101 type stuff and yet its not working.
Example:
// define global variables
var username;
// create a function that sets the variables to a value
function sessionCheckup() {
chrome.storage.sync.get(['username'], function(data){
username = data.username;
});
}
// a function that should return the variables.
function returnVariables () {
chrome.tabs.query({active: true,currentWindow: true}, function(tabs) {
sessionCheckup();
console.log(username);
});
}
returnVariables(); //console.log returns undefined
However if I do this it works just fine.
// define global variables
var username;
// create a function that sets the variables to a value
function sessionCheckup() {
chrome.storage.sync.get(['username'], function(data){
username = data.username;
});
}
sessionCheckup();
// a function that should return the variables.
function returnVariables () {
chrome.tabs.query({active: true,currentWindow: true}, function(tabs) {
console.log(username);
});
}
returnVariables(); //console.log returns the username variable value
Then continuing on with what I perceive as weird behavior this also doesn't work.
// define global variables
var username;
// create a function that sets the variables to a value
function sessionCheckup() {
chrome.storage.sync.get(['username'], function(data){
username = data.username;
});
}
// a function that should return the variables.
function returnVariables () {
chrome.tabs.query({active: true,currentWindow: true}, function(tabs) {
chrome.storage.sync.get(['username'], function(data){
username = data.username;
});
console.log(username);
});
}
returnVariables ();//console.log returns undefined
In standard javascript (not chrome extension) all three of these would return the same result.
So as the topic suggests I am really trying to understand this and get my head around it. How exactly do I return the variable definitions from the first function inside the second function?