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) {
...
});
Related
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 5 months ago.
I'm trying to retrieve data from database and store the value into a variable, but it gives me undefined value which means variable is not storing the data.
var n1;
var reff=firebase.database().ref('login-demi/');
reff.on("child_added", function(data){
var newnum=data.val();
n1=newnum.number; //4
if (n1==4){
n1=newnum.age;
}
});
console.log(n1);
But if I try to "put console.log(n1)" inside the function under "n1=newnum.age;" then it will display the value, but i want the variable to store the value and display it outside the function can I know how can I do that?
I think the problem is that the function "reff.on" is an event function and what happens is that you print the n1 variable before the function is executed.
Therefore, you will only be able to access this data from within the function, Or you will build a specific function to verify that the previous function has been executed before you print the n1.
Notice how they only access the information from within the function in the Firebase documentation : https://firebase.google.com/docs/reference/js/v8/firebase.database.Reference#parameters
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:
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
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.
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 8 years ago.
I am trying to get the response from ajax call and save it in another value but wont work
this is my code
x='go'
$.post("someurl",function(data){
x=data;
})
alert(x)//go
or this
jsFiddle
I am looking for a solution for this problem without storing the value in any HTML container
$.post is asynchronous - the callback will be executed later, after your alert(x) line.
Try:
$.post("someurl",function(data){
x=data;
alert(x)
})
(No, there's no other way around this - you'll have to restructure your code accordingly. Don't be tempted to try setting async to false, or you'll end up with bigger problems).
In Javascript when you make this ajax call you are sending an Asynchronous call to the "someurl". This means your function continues and x remains undefined.
Solutions:
$.post("someurl",function(data){
//use your data here
});
or define a function outside
var myFunction = function (data){
//do stuff with data
}
$.post("someurl",myFunction);
why first declare x as a string and then put data in the same x?
I think you should use json and have your php file parse it into json before sending back. Otherwise it just wont give a response. It will on check whether it has executed the call and that will always be "true" regardless of its succes.
Hope this helps.
BIEG