2

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?

Drew Gaynor
  • 8,292
  • 5
  • 40
  • 53
Jovin
  • 49
  • 2
  • 4
  • 1
    Read about promises a little bit... – Pavel Gatnar Oct 14 '15 at 19:43
  • and read about using function references – charlietfl Oct 14 '15 at 19:43
  • 2
    These are the droids you are looking for... https://promisesaplus.com/ – TbWill4321 Oct 14 '15 at 19:45
  • All right I got a basic understanding of the promise object right now. But nobody has answered my 2nd question yet of why is the var dataHolder = randomAngularService.getsomeData(); not work as it would in c# but it would perfectly get the data using the function with a promise object? I can't get my head around that. – Jovin Oct 14 '15 at 21:14

2 Answers2

2

It's used to replace (or provide an alternate way) the old callback mechanism with a cleaner way to handle asynchronous requests, instead of passing your callbacks as parameters, you can chain your function with .then, given function will be executed once the promise gets resolved.

Anyhow, this is just a basic explanation, you should really get into the books of promises for more info.

taxicala
  • 21,408
  • 7
  • 37
  • 66
1

I'm lazy to explain the whole promise thing, but just to answer question about .then

The 2 arguments inside .then actually means "call this function when the promise is resolved(success)/rejected(failed)"

About the arguments inside the functions, they should be specified in the API, because the caller (for instance $http.get) get to decide what to pass when calling that function.

Icycool
  • 7,099
  • 1
  • 25
  • 33