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]).
Related
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 4 years ago.
i have a function insde a function and im trying to return somehthing wich i return in the inner function.
function calculatePoints(){
request({
success: onWiktionaryResponseAvailable,
error: null,
url: "https://en.wiktionary.org/w/api.php?action=query&origin=*&format=json&titles="+inputWord,
});
function onWiktionaryResponseAvailable(result){
let wiktionaryEntry = JSON.parse(result),
keys = Object.keys(wiktionaryEntry.query.pages);
if (keys[0] === "-1"){
return 0;
}return inputWord.length;
}
}
i have tried to write: return onWiktionaryResponsAvailable(); but that doesn´t work in my case.
You've already got the idea of a "callback" function here when you call your request. You just need to carry that one step further, and pass in a callback function. (You could also accomplish the same thing with a Promise)
function calculatePoints(callback) {
request({
success: onWiktionaryResponseAvailable,
error: null,
url: "https://en.wiktionary.org/w/api.php?action=query&origin=*&format=json&titles=" + inputWord,
});
function onWiktionaryResponseAvailable(result) {
let wiktionaryEntry = JSON.parse(result),
keys = Object.keys(wiktionaryEntry.query.pages);
if (keys[0] === "-1") callback(0);
callback(inputWord.length);
}
}
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 7 years ago.
I have json data in file that I want to read. It prints to the console.log the correct file data.
How do I get that data into variable x? When I run the code, x is undefined.
function getData() {
$.get('data/APPFeaturesMetaData.json', function (data) {
console.log(data);
return data;
});
};
var x = getData();
It's an asynchronous call, So the var x = getData(); runs before the AJAX request is complete.
The answer is, use deferred it looks something like this:
var request = $.ajax(
{
url: url,
});
// When the request is done, do something with data.
request.done(function (data) {
console.log(data);
});
In your case, it's different if you want to return data, you will have some scoping problems. the fix is really easy, Here's what your final code will look like, I will explain stuff in the comments:
function getData() {
// This is where we store the data.
var myData;
// This will refference current object.
// You need to keep the refference of this object.
var self = this;
$.get( 'data/APPFeaturesMetaData.json' ).done(function(data) {
// the keyword `this` will not work here, because you're inside an AJAX callback.
// When you say `this`, you're actually refferencing the AJAX object
// And not the getData object.
self.myData = data;
});
return myData;
};
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;
});
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 8 years ago.
I am writing a callback function in node.js but it is returning me an undefined value. Below is the code
exports.getRecord = (function(callback) {
return function(req, res) {
var recordName;
DBObject.record.find(jsonString, function(err, doc_record) {
doc_record.forEach(function(docRecordTravel) {
recordName = callback(docRecordTravel.recordCode);
console.log(recordName);
})
}
})(callbackFunc);
function callbackFunc(recordCode) {
var recordName;
DBObject.var_recordRack.find({
recordID: recordCode
}, function(err, record) {
record.forEach(function(recordLoop) {
recordName = recordLoop.recordName;
});
console.log("callback " + recordName);
return recordName
});
}
In callbackFunc it is showing me the recordName but when i return it it displays undefined. how can I return the value in call backs in node.js.
You can't.
It uses a callback function because it is asynchronous.
By the time the callback is called, the previous function has already finished and there is nowhere to return the value to.
If you want to do something with the data, then you must do it from the callback function.
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?