Vlad
Vlad

Reputation: 1308

AngularJs promises: can you use both success and then?

I feel this is a very bad idea as i do not find it anywhere but testing it on my localhost it seems to work. I have a service :

angular
    .module('trips.services')
    .service('Trip', Trip);

function Trip($http, $q) {
    //.....

    function create(data) {
        var api_url = "/api/trip";
        return $http.post(api_url, data).success(function(data) {
            mixpanel.track("Trip: created", {}); // track that a new event happened
        });
    }
}

I also have a controller where i do something like:

Trip.create(data).success(function(data) {
    //do something after the trip is created
})

Now as you can see in this example I use both the then promise and the success callback in the service. If i do breakpoints in the debugger the success callback from the service is executed first and then the then clause from the controller. And it is also great for code organization, the common things i do after the service is successful is in the service while the specific things are in the controller. The thing is, i feel it is very non-according to any documentation or examples. Did not really find it somewhere, I discovered it by mistake. It might not work in some cases also?

I know the other options also, the $q library and could deffer in the service but that ends up in much more code.

So, is this a bad idea and why? Any opinions would be very helpful. I do not want to set up a wrong practice.

Upvotes: 1

Views: 269

Answers (2)

LionC
LionC

Reputation: 3106

Using promises consequently is considered a good practice as you already know. To do that, you can simply replace your success-callback with another then:

function create(data) {
    var api_url = "/api/trip";

    var promise = $http.post(api_url, data);

    promise.then(function(data) {
        mixpanel.track("Trip: created", {}); // track that a new event happened
    });

    return promise;
}

Having multiple independent .then()s on one promise is perfectly fine and works as you would expect.

Upvotes: 2

SoluableNonagon
SoluableNonagon

Reputation: 11755

It's not necessarily a bad idea, it's just you using chaining of promises.

The .then function looks like this:

.then(function(success){}, function(error){});

you can see that .success is just shorthand for the first function above (as if you didn't declare the error callback).

Seeing as the promise can be passed around or maybe you have multiple services making the same request and you want just one promise created, then you might like to declare callbacks from multiple areas yet have just one promise resolve.

So is it a bad idea? No. Why? It is a flexible piece of code which is really up to the programmer to use. Can you declare multiple thens? Yes. Should you mix it with success callbacks? You can, but you might just want to stick to one style for consistency. But thats really up to what you're trying to achieve.

Angular documentation: https://docs.angularjs.org/api/ng/service/$http
(Watch out for deprecation, might just want to stick to .then)

Upvotes: 1

Related Questions