In the process of developing a mobile application using Appcelerator Titanium, I find myself sending various xhr requests. While this isn't specifically related to Appcelerator Titanium, any code suggestions provided should be in Javascript.
The app requires authentication for certain interactions with the user, among other things.
At this stage, every request may yield different responses such as:
- not authenticated
- not logged in
- invalid parameters
- successful
- ...
These requests are enclosed within different model methods or helpers.
However, since this is new territory for me, I'm curious about the best practices to follow.
For instance, some practical questions include:
If the app lacks authentication (e.g., token expiration), should it attempt authentication and then resend the denied request? (while keeping the process transparent to the user)
Should an authentication request be sent each time the app launches, followed by "forgetting" it?
The issue here is that addressing each request individually leads to lengthy code with nested callbacks, retry logic, event listeners, etc. It doesn't feel efficient or maintainable, as what's necessary is a streamlined approach where any request is checked for errors, rectified automatically if possible (through authentication or automatic login), and retried multiple times before giving up if necessary.
I've heard about the promise pattern but only understand it theoretically, unsure if it aligns with my requirements.
Hence, I would appreciate any guidance on tackling this specific challenge. I'm curious how apps like "Facebook" manage similar scenarios.
Thank you for your assistance.