There is not much answer for this simple question that I have. My main question is that I have seen the .then
method used a lot in JavaScript and I know the main thing where randomobject.then(//This returns success, //this returns failure)
. But there are things that I don't get such as the code here:
var success = function (response) {
return response.data;
};
var error = function (errResponse) {
$log.error(errResponse.data.Message);
};
function getsomeData() {
var url = baseUrl + 'api/somedata';
return $http.get(url).then(success, error);
}
First off in that code I'm wondering how the var success knows what data it is getting and the same with error. It says response.data but what is response? It's probably the result of the http.get
but that doesn't make much sense code wise. Also it seems that when I have a function for example.
getsomeData
returns what it returns. Why doesn't it work if I do the ff:
var dataHolder = randomAngularService.getsomeData()
it returns an object that holds a data under $$state
but somehow the .then
makes it work if you do the ff:
randomAngularService.getsomeData().then(function (response) {
if(response != null) {
console.log('got the data');
$scope.meeData = response;
}
});
I thought the .then
only takes two parameters? This is what's confusing me.
Also is the .then
property a JavaScript method or a jQuery one?