This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 8 years ago.
I want to store a value from a JSON object in a global variable to use later in my code. Apparently this does not work even though it should make the country variable global.
$.getJSON(reverseGeoRequestURL, function(reverseGeoResult){
window.country = reverseGeoResult.results[0].locations[0].adminArea1;
});
console.log(window.country);
The getJSON call is asynchronous. Put that console.log call inside the callback and it will not be undefined.
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)
Closed 2 years ago.
Can somebody explain me how to solve this problem?
let konyvSzam=0;
db.checkPeldanySzam(rentISBN,(konyv) => {
konyvSzam=konyv.Peldanyszam;
});
console.log("Nr of books");
console.log(konyvSzam);
The variable gets the value but after I check it doesn't work
Mostly our DB call works asynchrous in node js and JavaScript try to enclose db calls in promise or use async/await .
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 5 years ago.
How do I access a variable inside a callback without reassigning it to a variable first?
For example, the following code works:
let volume = 0;
loudness.getVolume((err, vol) => {
volume = vol;
});
But what if I wanted it to be assignable directly to a const. The following returns undefined:
const volume = loudness.getVolume((err, vol) => vol));
The short answer is you can not. The callback function exists in it's own scope isolated from the rest of the code. The only way to extract that info to be used in the rest of the code is to assign it to a variable that exists in a parent scope.
In simple terms do what you did in your first example.
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:
How do I return the response from an asynchronous call?
(41 answers)
Closed 8 years ago.
I have been struggling for quiet sometime now with conversion of file chunks to binary data and have come to this point
for(....){
$.when(chunkBinary(chunk[i][j])).done(function(result){ chunkInBinary = result;console.log(chunkInBinary);} )
}
Now I want to be able to use the value chunkInBinary outside the done function. Something like this:
for(....){
$.when(chunkBinary(chunk[i][j])).done(function(result){ chunkInBinary = result;} )
console.log(chunkInBinary);
}
Any suggestions on how I can achieve this?
My chunkBinary function returns a promise.
What you have done is already correct.. by creating a variable without the var keyword, u r literally creating a global variable. So now you can access the chuckinbinary variable outside any function as it is in global scope. This is not good practise as u r cluttering ur global work space.