Don’t use UI-Router’s resolve

Let’s say you have an app with certain permissions. And let’s say the permissions can usually be determined by the URL. For example, when each URL encodes a resource and you’ll get from the server which permissions the current user has for this resource. Isn’t it then a great idea to use resolve in UI-router, so you don’t have to deal with those pesky promises anymore?

I mean what you’re doing with resolve:

  1. Get the permissions and wait app-wide until they are loaded
  2. Load the view. Then you can treat permissions synchronously like permissions.EDIT_RESOURCE
    instead of the cumbersome

    PermissionService.then(resp => {
      //do everything permission related here
    }

    which makes your app more nested, especially when you have multiple promises.

So why, when there are such great advantages, shouldn’t you use UI-Routers resolve?

The reason for this is, your app will feel very laggy if you start using the resolve excessively. Because before any state transition, the page will resolve the promise. Meaning until you get the response from the server, your app won’t do a thing. So for example you click a button to enter a resource, but absolutely nothing happens until the resolve’s are resolved. This feels very laggy and broken. Trust me, I speak from experience. The app will feel so much more responsive if you use the promises. You’ll have a bit more code, but usually it’s worth it, since parts of your app can be rendered much faster and the other calls don’t have to wait for the promise to resolve.

Leave a Reply

Your email address will not be published.