Javascript 角度-包含在一个承诺中的多个承诺

Javascript 角度-包含在一个承诺中的多个承诺,javascript,angularjs,angular-promise,Javascript,Angularjs,Angular Promise,我有两个服务,它们通过对象上的标志返回相同类型的对象,只是不同类型的对象。这些服务具有返回承诺的功能 服务1-获取地点: this.getPlaces = function () { return $http.get('http://somewhere.com').then(function (response) { return response; }).catch(exception.catcher('Somethin

我有两个服务,它们通过对象上的标志返回相同类型的对象,只是不同类型的对象。这些服务具有返回承诺的功能

服务1-获取地点:

this.getPlaces = function () {
            return $http.get('http://somewhere.com').then(function (response) {
                    return response;
     }).catch(exception.catcher('Something went wrong'));
};
this.getPlaces = function () {
            return $http.get('http://somewhere.com/').then(function (response) {
                    return response;
     }).catch(exception.catcher('Something went wrong'));
};
服务2-获取地点:

this.getPlaces = function () {
            return $http.get('http://somewhere.com').then(function (response) {
                    return response;
     }).catch(exception.catcher('Something went wrong'));
};
this.getPlaces = function () {
            return $http.get('http://somewhere.com/').then(function (response) {
                    return response;
     }).catch(exception.catcher('Something went wrong'));
};
我还有一个调用这两个服务的包装器服务,我不希望服务包装器中的函数返回组合结果,直到这两个承诺都完全执行并返回结果(或null)。我目前的做法如下:

    this.getCombinedResults = function () {
        var combined= [];

        var apiCalls = [service1.getPlaces(), service2.getPlaces()];

        $q.all(apiCalls.map(function(call) {
            return call;
        })).then(function (response) {
            angular.forEach(response, function (item, index) {
                combined.push(item);
            });
        });            
        return combined;
    };
这是在一个承诺中包装多个承诺的正确方法吗?如果不是,应该改变什么。我认为它不能正常工作,尤其是在我的包装服务中。调用包装器服务函数时,它返回null


我将对此进行更多的研究,但如果我能知道我所写的是否正确,那就太好了。谢谢。

由于
getCombinedResults
正在调用基于承诺的服务,此服务也应返回承诺,否则combined将始终为空

照办

 return $q.all(apiCalls.map(function(call) {
            return call;
        })).then(function (response) {
            angular.forEach(response, function (item, index) {
                combined.push(item);
            });
            return combined;
        }); 
因为
then
也会返回一个承诺,所以此代码将向调用者返回一个承诺

解析的值将是
组合的


另外,在调用代码中,您需要使用基于承诺的响应解析,使用
然后

在stackoverflow上发布后,我实际上刚刚在代码中发现了这一点。收到一个新错误-无法读取undefinedIt的属性'then'。很好,我忘记了$q.all的返回值。我现在觉得自己很傻。我所做的是正确的方法吗?我可能不应该在我的架构中一路回馈承诺?但如果我不这样做,我怎么能在一个$q.all中包装多个电话——它们必须是承诺吗?是的,相信。您可以使用
$q.when(value)
包装非基于承诺的数据。