service returns promise 但 cachingService 没有

service returns promise but cachingService not

我正在调用 getWeeklyDates,它正在调用 cachingGlobalConfigurationService,如果在本地存储中找不到 globalConfiguration 数据,它会再次调用 globalConfigurationService。

代码=>

return cachingGlobalConfigurationService.getGlobalConfiguration()
                   .then(function(response1){

在尚未缓存 globalConfiguration 时工作正常,因为那时我会进行 ajax 调用并 return 做出承诺。

但是当我的 globalConfiguration 可以在 localStorage 中找到时,上面带有 .then(function(response1) 的代码行是未定义的,只是 returned:

 else {
          return cachedGlobalConfiguration;
        }

我想我不能在这种情况下使用 .then 但我愿意。

我该如何解决?

1

this.getWeeklyDates= function (projectId, currentDate) {

        return cachingGlobalConfigurationService.getGlobalConfiguration()
               .then(function(response1){

        // do business logic
    });

2

'use strict';
angular.module('test').service('cachingGlobalConfigurationService', function (localStorageService, globalConfigurationService) {
  this.getGlobalConfiguration = function () {

    var cachedGlobalConfiguration = localStorageService.get('globalConfiguration');
    if (!cachedGlobalConfiguration) {
      return globalConfigurationService.getGlobalConfiguration().then(
        function (globalConfiguration) {
          localStorageService.set('globalConfiguration', globalConfiguration);
          return globalConfiguration;
        },
        function (error) {
          console.log('error', error);
        });
    }
    else {
      return cachedGlobalConfiguration;
    }
  };

  this.saveGlobalConfiguration = function (globalConfiguration) {

    // TODO: Only save to local storage when service.save was successfully
    localStorageService.set('globalConfiguration', globalConfiguration);
    globalConfigurationService.saveGlobalConfiguration(globalConfiguration);
  }
});

3

'use strict';
angular.module('test').service('globalConfigurationService', function ($http) {
  this.getGlobalConfiguration = function () {

    // TODO get from db
    var path = 'scripts/model/globalConfiguration.json';
    return $http.get(path).then(function (response) {
      return response.data.globalConfiguration;
    });
  };

  this.saveGlobalConfiguration = function (globalConfiguration) {

    // TODO: save on db
    //var path = 'scripts/model/globalConfiguration.json';
    //return $http.post(path, globalConfiguration).then(function (response) {
    //  alert('global configuration was saved succesfully!');
    //});
  }

});

你可以注入 $q 服务并在返回时使用 $q.when 包装对象,这样你总是从你的 api 返回一个承诺(并且只是删除了多余的别的)。还请记住从承诺的 catch 回调中拒绝承诺(如果需要)。

'use strict';
angular.module('test').service('cachingGlobalConfigurationService', function (localStorageService, globalConfigurationService, $q) {
  this.getGlobalConfiguration = function () {

    var cachedGlobalConfiguration = localStorageService.get('globalConfiguration');
    if (cachedGlobalConfiguration) {
        //Return a promise
        return $q.when(cachedGlobalConfiguration);
     }

     return globalConfigurationService.getGlobalConfiguration().then(
        function (globalConfiguration) {
          localStorageService.set('globalConfiguration', globalConfiguration);
          return globalConfiguration;
        },
        function (error) {
          console.log('error', error);
          return $q.reject(error); //<-- reject
        });
  };

  //....
});

$q.when - Wraps an object that might be a value or a (3rd party) then-able promise into a $q promise. This is useful when you are dealing with an object that might or might not be a promise, or if the promise comes from a source that can't be trusted.