Home Stop a promise chain without using reject in AngularJS
Reply: 0

Stop a promise chain without using reject in AngularJS

user1322 Published in June 19, 2018, 2:43 pm

According to this blog post, it's possible to return null in a resolver function to skip over subsequent then handlers in a promise chain. I'm curious as to how this works -- nothing in the Promises/A+ spec defines this behavior that I can see.

For my use case, I adapted their example to handle rejections from the $uibModal modal dialog service in Angular UI Bootstrap, which resolves or rejects depending on whether the modal is closed or dismissed:

  // ... modal options
.catch(function () {
  // Modal dialog was dismissed, so use this trick to stop the promise chain
  return $q(function () { return null; });
.then(function () {
  // ... this will not get executed if dialog was dismissed
  return someAPIcall();
.catch(function () {
 // ... handle errors with API call

This is nice because I don't have to check the error value (the $uibModal API uses various strings for modal rejection types) in the first catch or use complex branching/nesting to avoid mixing the handling of modal dismissal rejections with errors produced by someAPIcall().

But how does it work? Where is this documented?

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.300947 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO