This question already has answers here:
Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference
(7 answers)
Closed 6 years ago.
I am setting chrome.local variables in pop.js but unable to retrieve them in content.js
popup.js
chrome.storage.local.set('TITLE',title);
content.js
var title = null;
$(".class").each(function () {
chrome.storage.local.get('TITLE', function (result) {
title = result.TITLE;
console.log('inside'+title);//prints value
});
}
console.log(title);//returns null;
It returns title as null
Chrome Console outputs as:
outside: null
content.js:42 insideJava: A Beginner's Guide, Sixth Edition
chrome.storage.local.get is an asynchronous call, which means when you call console.log(title);, callback of chrome.storage.local.get has not been executed, then title remains null. You should put your logic inside the callback.
Related
This question already has answers here:
How to return value from an asynchronous callback function? [duplicate]
(3 answers)
Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference
(7 answers)
Closed 1 year ago.
I need to get a local storage value outside the call, to use it in another function,
I see this is an async function and outside log gets called first, but cant rly wrap how it works and why I get the value on the second call, but not it the first call.
So the question is how do I get the value outside the call and make First Call work as second call ?
var Credentials = []
chrome.storage.local.get(["Key"], function(result) {
const o = result.Key.email
console.log("this is email", result.key.email)
Credentials.push({"email": result.key.email})
console.log("Second Call",Credentials[0].email)
});
console.log("First Call",Credentials[0])
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference
(7 answers)
Closed 5 years ago.
I have a function that I call which hits an API and returns a JSON, which I am calling getData. Inside that function is a nested function that processes the JSON and saves it to a new variable processedData when it's done making the request from the API.
I'd then like to return that processed JSON, but the console is telling me it's undefined. I didn't declare the variable so it should be treated as global. Why am I still having this issue?
The code won't work with the snippet because the API is only local, but processedData essentially looks like this: {'A': '123'}
function hitAPI() {
var getData = $.get('http://127.0.0.1:5000/myroute');
getData.done(function() {
processedData = (JSON.parse(getData['responseJSON']['data']));
});
return processedData;
};
var x = hitAPI()
console.log(x);
This question already has answers here:
Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference
(7 answers)
How do I return the response from an asynchronous call?
(41 answers)
Closed 6 years ago.
I have a promise/global object that outputs as follows in the console:
console.log(globalInfo);
Promise
__proto__:Promise
[[PromiseStatus]]:"resolved"
[[PromiseValue]]:globalInfo
item1:"12345"
item2:"something else"
item3:"www.stackoverflow.com"
__proto__:Object
And am successfully pulling out the value for "item2" as such:
globalInfo.then(function(val) {
newVar = val.item2;
console.log('newVar inside: ' + newVar);
});
console.log('newVar outside: ' + newVar);
But, I am not able to pass that variable/value outside of the then() function. Nothing outputs to the console for the second console.log ("newVar outside").
How do I make the variable "newVar" with the the value for "item2" available globally outside of the then() function?
This question already has answers here:
Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference
(7 answers)
Closed 6 years ago.
I would like to assign value to the variable IMLoginReq inside a ProtoBuf load function, but its not working, can anyone help?
var IMLoginReq;
protobuf.load("./pb/IM.Login.proto", (err, root) => {
// Obtain a message type
IMLoginReq = root.lookup("IM.Login.IMLoginReq");
console.log(IMLoginReq);//<== is not undefined
});
console.log(IMLoginReq);//<== is undefined
The load() method is asynchronous. As such the console.log at the end will happen before the load finishes. Instead of trying to treat this as procedural logic, which it is not, you should instead use the IMLoginReq inside the success method that you have.
Thats because you are trying to call it before its loaded. You should have a callback function like success so it will be there.
You can also use promises by omitting the callback:
protobuf.load("awesome.proto")
.then(function(root) {
...
});
This question already has answers here:
Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference
(7 answers)
Closed 6 years ago.
I'm having problems trying to add rows to the variable x. I have not understood how to return a value from an anonymous function called by another function.
var pgClient = new pg.Client(connectionString);
pgClient.connect();
var query = pgClient.query("SELECT * from sceglie");
var x = [];
query.on("row", function(row,result){
result.addRow(row);
x.push(row);
});
console.log(x);
There is no way to do what you are trying to do. Javascript is a synchronous language in that it executes line by line. So this line:
console.log(x)
Will execute right away. The "on" function will only execute when it is called, so your log statement will always have the empty value.
Link to good article explaining JS environment