0

I'm trying to find out an answer to my question - does

myFunction()
        .then(function (response) { })
         .catch(function(error) { });

is the same as

myFunction()
        .then(function (response) { },
        function(error) {}); 

to handle errors from the promise? If so which one should be used as best practice?

5
  • The default promise type for angular is $q and per the documentation docs.angularjs.org/api/ng/service/$q the two are the same. However, this is only for promises generated from angular. There are other promise libraries out there. Commented Sep 22, 2017 at 18:44
  • @losSteveos can you provide some useful examples? Commented Sep 22, 2017 at 18:48
  • I use first one, I find it much nicer and cleaner to read. Commented Sep 22, 2017 at 18:48
  • 1
    The industry is the process of changing out promisses. It's actually been added to the spec in es6. developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… is a basic intro, developers.google.com/web/fundamentals/getting-started/primers/… talks about poly-fills because not all browsers support native promise yet. Commented Sep 22, 2017 at 18:49
  • 1
    For additional libraries out there I suggest doing a search on npmjs.com Commented Sep 22, 2017 at 18:52

1 Answer 1

1

Yes, with they both you will get the same result.

From $q service

catch(errorCallback) – shorthand for promise.then(null, errorCallback)

About to which one to use as a best practice... It's a matter of perspective: IMHO, you can use them both indistinctly.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.