Undefined global variable when returning data generated in a nested function [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 years ago.
I have a function that I call which hits an API and returns a JSON, which I am calling getData. Inside that function is a nested function that processes the JSON and saves it to a new variable processedData when it's done making the request from the API.
I'd then like to return that processed JSON, but the console is telling me it's undefined. I didn't declare the variable so it should be treated as global. Why am I still having this issue?
The code won't work with the snippet because the API is only local, but processedData essentially looks like this: {'A': '123'}
function hitAPI() {
var getData = $.get('http://127.0.0.1:5000/myroute');
getData.done(function() {
processedData = (JSON.parse(getData['responseJSON']['data']));
});
return processedData;
};
var x = hitAPI()
console.log(x);

Related

putting get chrome storage local value to a dict [duplicate]

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])

Javascript json object is empty after loop block [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.
var jsonObj= [{}]
function read_directory(){
foreach files in directory{
//do stuff to file
jsonObj.push(newdata) //newdata is also an json object
console.log(jsonObj) //this returns the current object after each push
}
console.log(jsonObj) //this returns an empty json object
}
Why does the object appear to be empty when it is called outside of the loop.
When it is called inside of the loop, the data is pushed correctly. But when I want to save the complete object after the loop it is empty.

How do I access data outside of the then scope when resolving a promise? [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.
Noob question here. Can someone elaborate on why seriesData variable outside the then scope is still an empty object. Additionally, what's the appropriate way of accessing data outside of said scope?
fetchData is a method that returns an array of three objects when successfully resolved.
Obviously, the console.log inside the then scope prints the expected seriesData object with the resulting data from the resolved promise.
Here's the code.
import fetchData from './data';
const seriesData = {};
fetchData()
.then(res => {
seriesData.characters = res[0];
seriesData.locations = res[1];
seriesData.episodes = res[2];
console.log(seriesData);
})
.catch(error => console.error(error.message));
console.log(seriesData);

How save in ouside variable the result is coming on petition using jsonp [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.
hello to all I am new on this I try save the result on outside variable to use late on coming from a petition using jsonp
var user = null;
gettingUser()
function gettingUser() {
$.getJSON("https://myapi.com/users/profile/?userid=" + id + "&jsonp=?", function (json) {
getData(json);
});
console.log(user) // this is null here why ?
}
getData(data){
user = data
}
all ways I get null in the var user I need help thanks

Retrieving JSON array with $.get but can't set retrieved array value outside function [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 5 years ago.
If I do the following it gives me, in console.log, the values I want.
var q;
$.get('/ajax_subscribers', { code: 'qyhskkcnd'},
function(returnedData){
q = returnedData;
console.log(q);
});
But if I do this
var q;
$.get('/ajax_subscribers', { code: 'qyhskkcnd'},
function(returnedData){
q = returnedData;
});
console.log(q);
q has no value and is an undefined value? Why can't I set returnedData as public?
$.get is asynchronous. You are logging before the ajax is able to finish. In other words, the var q will only have the value once the $.get receive the response from the service.
You should handle the returnedData inside the callback function. Or delegate to another function if it's too complex, like:
$.get('/ajax_subscribers', { code: 'qyhskkcnd'},
function(returnedData){
doStuffWithReturnedData(returnedData);
});
Your $.get() is asynchronous.
To get around that, try setting a Promise.

Categories