0

I have a such situation:

  1. Have a service 'MyService'

function getData() {

  // $timeout(function () {
  if (data) { // What to verify if data exist for not to make a request
    var defer = $q.defer();
    defer.resolve({
      data: data
    });
    return defer.promise;
  }
  // }, 0);

  return ApiWrapper.resolve('get', 'api/data'); // Just a wrapper for request, return a promise
}
  1. Get request to server in run block and save response to my service 'MyService'

getData().then(function(response) {
  MyService.data = response.data;
});
  1. In ui-router resolve invoke same method getData, but if data already exist, I don't want to make request to the server, I want get stored data from service. But situation is that my Controllers do same request few times while loading and app.

How to refactor MyService for make only one request to server. Or, is there any other solutions?

Odinn
  • 1,106
  • 9
  • 28
  • Possible duplicate of [Caching a promise object in AngularJS service](http://stackoverflow.com/q/18744830/1048572), although I'm not sure what you are asking – Bergi Apr 20 '17 at 08:42
  • thanks, but It almost the same I've got in my solution in getData function, I've got 3 get requests 'couse there is no data yet – Odinn Apr 20 '17 at 09:46

0 Answers0