3

Sometimes I see more than two functions separated by comma, inside of promise then() in AngularJs. Could anyone here help to explain what the structure mean?

e.g.

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

I understand that, if there are two functions inside of then(). The first function will run if it fulfill the promise, otherwise the second will run if there is any error occurs. This structure doesn't look like chained promise either...

Thank you very much for any help here :-)

1
  • 2
    As you can learn in the docs, it's a notifyCallback. Commented Oct 14, 2015 at 16:28

1 Answer 1

4

Well:

  • The first function is the fulfillment handler, it will execute when the promise resolves to a value (normally).
  • The second function is the rejection handler, it will execute when the promise resolves with an error by rejecting (or throwing an exception in a handler).
  • The third function is for progress, this is a nonstandard and deprecated feature that will never be a part of ECMAScript promises - it is best to avoid it altogether since it does not compose well. It is also not supported in Angular's new $q API.
  • Anything past the third function passed in is ignored by the handler and will have no effect.

Here is an example for all three being called:

var good = $q.when(3);
good.then(x => console.log("Successful"));
var bad = $q.reject(Error("Bad")); // always reject with errors
bad.then(null, x => console.log("Failed"));
var d = $q.defer(); // don't do this like... ever
d.promise.then(null, null, x => console.log("Progressed"));
d.notify("event notification"); // never use this in real code ;)
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.