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 4 years ago.
I am attempting to access color options from a chrome extension, when I run this code, the second output is undefined, even though the variable is supposed to be able to be accessed by every part of the code because I defined it in the beginning.
var color;
function updateColor(){
chrome.storage.sync.get("favoriteColor", function(result) {
color = result.favoriteColor;
console.log(color);
});
console.log(color);
};
updateColor();
I have searched all over the internet and I am unable to come up with a solution, any help would be greatly appreciated :)
Related
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 4 years ago.
$scope.order_details=function(index){
var temp_order_id=$scope.order_data[index].order_id;
$http.get('http://web.com/app/order_details/'+temp_order_id).then(function(response){
$scope.order_details11=response.data.data;
console.log($scope.order_details11);//Working Fine
});
}
console.log($scope.order_details11);//return undefined
This is an angularjs code on which I am working on when I console the array inside the $http.get(...) it is working fine but when I try to access that array outside the function it is returning undefined
By "Working Fine" do you mean it actually logged $scope.order_details11 to the console or do you mean the code just compiles fine?
If it is the later (code simply compiling), I think it's because you have not executed the callback $scope.order_details, only defined it, and hence the attributed order_details11 in $scope is non-existent.
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 6 years ago.
var feed;
$.getJSON('short.json', function(data) {
feed = data.items;
console.log(feed);
});
console.log(feed);
I have this short code written above. I am expecting feed to be a global variable but once it comes out the function, it's undefined again. It prints out an object when inside. What am I doing wrong?
Thanks for the help.
The reason is that the getJSON() call is asynchronous. It won't run until AFTER the second console.log();
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 7 years ago.
This may sound a newbie question but I'm having really hard time with variable scopes in Javascript.
I have the following JS snippet:
<script>
window.IDFVPlugin.getIdentifier(function(result){ uuid = result; });
alert(uuid);
</script>
I want to use the variable uuid anywhere in the script outside the window object. uuid returns the correct value only when inside the object and the value is lost when outside. So the alert above will log an undefined variable error.
You use a callback function. Result should be used inside of callback body. If you try to use it immediately after main function call - it will not be yet available
window.IDFVPlugin.getIdentifier(function(result){
uuid = result;
alert(uuid);
});
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 7 years ago.
var curline;
chrome.storage.local.get("value",function(item)
{
window.curline=item["value"];
});
alert(curline);
I want to set the curline with item["value"],this code is in theinject.js,thanks.
chrome.storage API is asynchronous. The callback is executed later, after you alert.
Which means you must alert the result in the callback you pass :
var curline;
chrome.storage.local.get("value",function(item)
{
window.curline=item["value"];
alert(curline);
// here you may use curline, or pass it as argument to other functions
});
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 8 years ago.
I'm pretty confused by the JavaScript scoping. I have a call to FB.api (shown below) that I would like to return a response object:
(function() {
var permissions;
FB.api("/me/permissions", "get", {access_token: options.token}, function(r) {
permissions = r;
});
console.log(permissions);
})();
Ideally, I'd like to assign FB.api to a variable, and use the anonymous function to return the r component, but it appears like that's not doable.
Why is permissions undefined? Is there a way to set permissions in the parent scope?