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?
Related
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;
}
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)
Console.log showing only the updated version of the object printed
(3 answers)
Closed 2 years ago.
I have this React project, in which I fetch data from a firebase database and construct a object called "statehelp" from the results. Now for some reason, if I console log the whole statehelp Object I get all its contents, however if I want to access statehelp.contents or statehelp.products they are undefined.
componentDidMount() {
var statehelp = {
content: {},
products: {}
}
getFarm(this.id).then(result => statehelp.content = result)
getProduct(this.id).then(result => statehelp.products = result)
console.log(statehelp)
console.log(statehelp.content)
}
That happens because console.log() is synchronous operation. It won't wait for your api call to happen. So don't use console.log() as a metric for identifying whether your object got populated or not.
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.
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?