Create promise in AngularJS

前端 未结 2 1913
春和景丽
春和景丽 2021-01-21 09:33

I\'m trying to create a promise in Angular with the $q service. It returns an object retrieved from a web service. If the object is in the cache, it returns it without calling t

相关标签:
2条回答
  • 2021-01-21 10:09

    You don't need to wrap it into one more promise since ServerRequest.getNavigationMap() is a promise:

    function returnMapAsync() {
    
        if (navigationMap) {
            return $q.resolve(navigationMap);
        } 
    
        return ServerRequest.getNavigationMap().then(function(data) {
            navigationMap = data.object;
            return navigationMap;
        });
    }
    
    0 讨论(0)
  • 2021-01-21 10:16

    You shouldn't need to wrap everything in the $q() call. In order to promisify navigationMap use $q.when:

    function returnMapAsync() {
    
        if (navigationMap) {
            return $q.when(navigationMap);
        }
        return ServerRequest.getNavigationMap();
    }
    
    0 讨论(0)
提交回复
热议问题