Not able to modify value in javascript callback [duplicate] - javascript

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 7 years ago.
I am not able to modify the value of outputString variable inside callback.
var outputString;
client.get(key,function(err,value){
outputString = "key="+key+" value="+value ;
console.log(outputString);
})
console.log(outputString);
When I print the value of outputString, it says "undefined"

client.get() returns immediately, so outputString hasn't been set when the last console.log() is called. The callback function isn't called until sometime later.

Related

How to access fetch data in javascript? [duplicate]

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 2 years ago.
I am just trying to access data inside then() method.
var dataset;
fetch ('https://raw.githubusercontent.com/freeCodeCamp/ProjectReferenceData/master/GDPdata.json').then (response=>response.json()).then(val=>{
dataset=JSON.stringify(val.data)
});
When i console out it outside then() method it give me this:
undefined;
But when I try to console it out inside , it give me my desired result;
I just want to store my desired result in my dataset variable. How?

javascript boolean variable's value is not changing [duplicate]

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 3 years ago.
I have a variable called itemExist and set that equal to false. My goal is to set this variable's value to true if a condition happens. My code is this:
var itemExist = false;
user.findById({_id: decoded._id}).populate('shoppingCart').exec((err, items)=>{
items.shoppingCart.forEach(async item=>{
if(item.productId == productId){
itemExist = true;
When I console log the itemExist variable outside of all theese functions
console.log(itemExist);
I get that result on the console:
false
But it should be true. How can I solve this problem, what is causing that?
Perform your work inside exec function
user.findById({_id: decoded._id}).populate('shoppingCart').exec((err, items)=>
{
const itemInArr = items.shoppingCart.find(item => item.productId === productId);
//!! - convertation to boolead value
itemExist = !!itemInArr;
console.log(itemExist);
}
);

global variable in javascript not working; loses data [duplicate]

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();

How to set the variable with chrome.storage.local.get in chrome [duplicate]

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
});

Getting a Javascript variable out of a $.get statement [duplicate]

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
How to return AJAX response Text? [duplicate]
(2 answers)
Closed 9 years ago.
EDIT: Please feel free to delete, I have found an appropriate answer in the duplicates mentioned above. Apologies.
I have the following code, and can't seem to dig the variables out correcly:
$('#button').click(function() {
alert(getRemaining(0));
}
function getRemaining(i){
var x;
$.get('files/remaining.txt', function(file){
x = file.split(",");
});
return x[i]
}
My alert just keeps coming out as undefined. What am I doing wrong?
the .get that you run is an asynchronous function. This means that execution of your code will continue on past it BEFORE it completes. The callback function that you pass into .get will be called once it is finished (this is the main reason for providing a callback).
This code will alert once the .get has returned.
$.get('files/remaining.txt', function(file){
x = file.split(",");
alert(x[0]);
});

Categories

Resources