Voxxrin is an OSS mobile-oriented application dedicated to conferences, allowing to browse presentations. Presentations may come from 2 sources :
- Either from a backend
- Or from localforage (if a previous call to the backend was made "recently", "recently" here corresponds to ~1day)
I'd like to implement a single method utility allowing to address both data resolutions at the same time, afterall, we should consider this as a single promise which may be resolved multiple times :
- Only once if localforage is empty (the first time we run the application)
- Only once if localforage is filled and we already called the backend "recently"
- Twice if localforage is filled and the backend was called some time ago, in that particular case, I'd like :
- To quickly return data coming from cache
- Run in the background a call to the server, and resolve the promise again once data is fetched (and in the case where backend is not reachable -for instance because user has connectivity issues-, user will still have presentations coming from cache)
I guess this is a very common pattern of "serve my data from the cache first, then try to refresh it from an http call"
Currently, I achieved to implement this by returning an "array of standard promises", because I didn't found any library on the www, allowing to achieve this easily. What I dislike in that approach is the caller (MyAngularCtrl) needs to wrap the method call (SharedData.loadPresentationsForEventPromises()) with an iteration, which :
- doesn't seem very natural
- imply to name the method weirdly : calling it loadPresentationsForEventPromises() instead of loadPresentationsForEvent()