undefine variable Displayed when implement below code [duplicate] - javascript

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 months ago.
undefine variable will loadData function from displayStructure all variables names, ages..etc displayed
async function loadData(route) {
const response = await fetch('http://127.0.0.1:8000/'+route);
const names =await response.json();
console.log(names);
return names;
}
function displayStructure(){
var names=loadNames('getNames');
var department=loadNames('getDep');
var ages=loadNames('getAge');
var bod=loadNames('getbod');
}

Related

.then cannot populate array 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 5 months ago.
I have problem with this function, it returns a console log, but does not push the data to the array, how can I fix this, I know the problem is related to promises but I don't know how I can fix it.
// Parse XLSX DATA function
function parseXLSXData(myFile) {
// Using the Read XLSX file cdn JS
let parseData = [];
readXlsxFile(myFile).then((rows) => {
// `rows` is an array of rows
// each row being an array of cells.
console.log(rows);
parseData.push(rows);
});
return parseData;
}

JavaScript and geolocation [duplicate]

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 1 year ago.
)
So I'm trying to learn about JavaScript and geolocation, but I've run into this, probably simple, problem:
let myLat; // Global
navigator.geolocation.getCurrentPosition((position) => {
myLat = position.coords.latitude;
console.log(myLat); // works
});
console.log(myLat); // undefined
I don't get why I cannot just use a global variable to hold the latitude-info?

Javascript: How to set value of variable from callback? [duplicate]

This question already has answers here:
How can I access the value of a promise?
(14 answers)
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 1 year ago.
Consider this scenario:
function myfunc() {
var total = 0;
const [ hooka, setHooka ] = useState([]);
const fetchData = () => {
api.get().then((data) => {
total = data.total;
setHooka(data.value);
}
}
return (
<h1>{total}</h1>
);
}
But the total inside the callback scope is different to the one defined in myfunc scope.
How can I set value of total in myfunc scope within the callback?
PS: It is a react hook in actual. Thank you TJ Crowder for the comment. And I've used total = data before a useState trigger method. I can use total as a hook also. But Doesn't creating more hooks slow down rendering in react or what?

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 json response undefined [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 5 years ago.
I have function like this:
function GetLala() {
var lala = $.getJSON(uriLala).done(function (data) {
return data;
});
return lala;
}
Then code like this:
var data = GetLala();
console.log(data.responseJSON);
This returned me undefined. If I type this code in google chrome console - then work.

Categories

Resources