Ionic race conditions and controllers

Hi All - I’m fairly new to Angular and trying to figure out how to structure my application, but my issue right now is with getting my first controller to (conditionally) wait to get a response from the server, if needed, before loading.
Basically I’m storing a user ID in local storage, and if that is present then my app can go ahead to the first controller (after an http response, for which I’m using promises). I’m not requiring a login because the data is not critical, it’s just a simple push service.

However, if that userID is not present (the user hasn’t registered), then I have to wait for ionic platform ready to ask for permissions to do push notifications. Upon receiving that permission , I hit my remote database to set up a new user. But by this time my first controller has already fired, with an undefined user and then errors.

I’m using UI-router and have tried a few approaches, including using resolve (with promises) on my first default controller. I had also tried adding a parent controller to do this work, since userID should be global, but then I don’t think my child controllers were waiting for that resolve to finish.

I feel like my code is a big mess right now (coming from the rails world) so general structure on that would be really appreciated as well. I have a user service which returns the user ID (or none), a separate register service which does the cordova pushplugin registration, and separate services that will either add or update the registration to my server (which hits my API services).

I’m struggling with the best place to put the user registration logic - it doesn’t really belong in a controller (unless maybe my parent app controller), and right now it’s in the router, which also seems wrong. Or maybe I’m overthinking this :smile:

Anyway, thanks in advance for any help.