How to return data from $.getJSON? [duplicate] - javascript

This question already has answers here:
How to return AJAX response Text? [duplicate]
(2 answers)
How do I return the response from an asynchronous call?
(41 answers)
Closed 9 years ago.
I created a function that loads game map from json file.
function newTileMapFromJSON(src) {
var mymap;
$.getJSON(src, function(data) {
[...]
var finalData = {
map: tMap,
tiles: blocks,
name: MapName
};
return finalData;
});
}
But it won't work. There is "return" but in function inside "getJSON". I've no idea how to get "finalData" and return it.
function newTileMapFromJSON(src) {
var mymap;
var finalData;
$.getJSON(src, function(data) {
[...]
var finalData = {
map: tMap,
tiles: blocks,
name: MapName
};
});
return finalData;
}
This code doesn't work, returned finalData is undefined. Maybe getJSON works in another thread? I don't know.
How would you do that, if you were me?

Related

Callback function for storing JSON to a variable [duplicate]

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 2 years ago.
I had a previous question where I was having issues for getting json from an url and storing it to a variable in order to pass it further to a function. I managed to get redirected to a solution that seemed promising, but it's still giving me undefined. The proposal was to use a callback as follows, but it doesn't seem to do the job.
var space = "";
function spaceCallback(data) {
fplan = data.space['space'];
}
function getSpace() {
var url = "http://localhost:8000/spaces/send_space/"
$.getJSON(url, function (data) {
spaceCallback(data);
});
}
I would need to pass space to blueprint3d.model.loadSerialized(space);.
What am I missing here?
Is it possible that you need to return your function?
function getSpace() {
var url = "http://localhost:8000/spaces/send_space/"
$.getJSON(url, function (data) {
return spaceCallback(data);
});
}
or ES6:
function getSpace() {
var url = "http://localhost:8000/spaces/send_space/"
$.getJSON(url, (data) => spaceCallback(data));
}

Returned object by function is empty [duplicate]

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 5 years ago.
I want return object with some data, but when I try return 'doc' variable, this variable is empty object.
here is my code:
myApp.factory('serviceDocuments', function serviceDocuments($http) {
var doc = {};
return {
getDocument: function (data) {
$http({
method: 'POST',
url: '/getDocument',
data: data
}).then(function successCallback(response) {
doc = response.data[0];
// in this place doc variable has needed data
}, function errorCallback(response) {
console.log("ups... ;(");
});
// in this place doc variable is empty object
return doc;
}
};
});
Your code is asynchronous. The line return doc is invoked before the callback for your http call (doc = response.data[0]).

Array contains elements, but length === 0 [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)
Is Chrome’s JavaScript console lazy about evaluating objects?
(7 answers)
Closed 5 years ago.
This code returns an array of strings:
var getCitiesList = function (url, callback) {
var citiesList = [];
var search = function (needSearch,whereSearch) {
var re = new RegExp(needSearch,'ig'),
matched = whereSearch.match(re); //возвращает массив совпадений
return matched !== null;
};
$.getJSON(url)
.done(function (data) {
$.each(data, function (index, value) {
if (search(input.val(), value.City)) {
citiesList.push(value.City);
}
});
});
return citiesList;
};
When I call it here, it is looks like empty array, but contains elements, array.length === 0
input.keyup(function () {
var cities = getCitiesList('../kladr.json');
console.log(cities); //[] but contains elements
console.log(cities.length);//0
});
How it looks in browser

Loop through an array of urls and pass them to get_json, return the new array of datas [duplicate]

This question already has answers here:
How to wait until jQuery ajax request finishes in a loop?
(5 answers)
How do I return the response from an asynchronous call?
(41 answers)
Closed 6 years ago.
I can't figure how to loop through an array of urls and pass them to getJSON, to finally return the new array of datas.
var data_num=[];
$.each(arr_urls, function( index, value ) {
$.getJSON(arr_urls[index], function (data) {});
data_num.push(data);
});
alert(data_num);
var data_num=[];
$.each(arr_urls, function( index, value ) {
$.getJSON(arr_urls[index], function (data) {
data_num.push(data);
alert(data_num);
});
});
Put your pusher and alert in the callback like this.
Why can't you just do:
var jsonData = JSON.stringify(arr_urls);

Factory that returns data after asynchronous call [duplicate]

This question already has answers here:
How to return value from an asynchronous callback function? [duplicate]
(3 answers)
Closed 8 years ago.
I have a service(factory) that uses another service to fetch data.Something like this:
factory('myFactory', function(anotherFactory) {
var factoryObject = {};
factoryObject.myMethod = () {
var newObjectToReturn;
// async call
anotherFactory.get(id, function(data) {
// do some processing
newObjectToReturn = data;
});
return newObjectToReturn;
}
return factoryObject;
});
Now, problem is of course, that because of asynchronous call, factoryObject.myMethod() always returns undefined, because return newObjectToReturn is first executed, and I can't simply return data. Is there any way around this ?
return the response data in callback
factory('myFactory', function(anotherFactory) {
var factoryObject = {};
factoryObject.myMethod = () {
var newObjectToReturn;
// async call
anotherFactory.get(id, function(data) {
// do some processing
newObjectToReturn = data;
},function(response){
return newObjectToReturn;
});
}
return factoryObject;
});

Categories

Resources