Javascript AngularJS 1.0.7中的嵌套承诺和$resources

Javascript AngularJS 1.0.7中的嵌套承诺和$resources,javascript,angularjs,Javascript,Angularjs,我需要在AngularJS 1.0.7中运行一个带有参数的函数“searchBoats(boatType)”。此参数是另一个函数parseBoatType的结果,该函数正在运行一个使用$resource调用API的服务。当boatType在资源中返回并带有承诺时,如何运行searchBoats 这就是我所尝试的: var parseURL = function() {

我需要在AngularJS 1.0.7中运行一个带有参数的函数“searchBoats(boatType)”。此参数是另一个函数parseBoatType的结果,该函数正在运行一个使用$resource调用API的服务。当boatType在资源中返回并带有承诺时,如何运行searchBoats

这就是我所尝试的:

    var parseURL = function() {                                                         
            var deferred = $q.defer();
            var promise = deferred.promise;
            promise.then(function success (result) {                    
                console.log(result);
                searchBoats(result);
            });

            deferred.resolve(
                parseBoatType()
            );                                                                                                                                                                                                                          
        };      
        parseURL();

var parseBoatType = function() {

        //  Do some stuff        
        // Calculate boatType calling a service that uses resource to call 
        // an API
        // I can convert this callback into a promise but still facing same 
        // issue
        BoatType.getBoatTypeByName({name: boatTypeParsed}, function success(result) {
                return result;
            });

        // The service method is called and the code is still running until 
        // the end of the function without waiting for the service result.
        // Then the promise.then code in the parseURL is executed and 
        // searchBoats is run with boatType undefined.                                  
    };

 // The service with the $resource call to the API
.factory('BoatType', 

  function($resource, SERVER_URL){          
    var boatTypes =
     $resource('http://' + SERVER_URL +'/:action', {action:'boat_types'}, {       
        query: {method:'GET', isArray: true},           
        getBoatTypeByName: {method:'GET', params:{action: 'getBoatTypeByName'}, isArray: false}
     });        
     return boatTypes;           
  }
  )

您可以从
parseBoatTime
函数中的
BoatType
资源返回资源
$promise
,并使用promise解析
parseUrl
延迟

首先从
parseBoatTime
函数返回承诺:

return BoatType.getBoatTypeByName({
    name: boatTypeParsed
  }, function success(result) {
    return result;
  }).$promise;
然后使用
BoatType
资源中的承诺解析
parseUrl
延迟的

parseBoatType().then(deferred.resolve);
下面是从您的问题中提取的完整代码,以及我提到的更正

var parseURL=function(){
var deferred=$q.deferred();
var承诺=延期承诺;
承诺。然后(功能成功(结果){
控制台日志(结果);
搜索艇(结果);
});
parseBoatType().then(deferred.resolve);
};
parseURL();
var parseBoatType=函数(){
//做点什么
//计算调用使用资源调用的服务的boatType
//API
//我可以将此回调转换为承诺,但仍然面临相同的问题
//发行
//ngResource@^1.2.0的代码
/*返回BoatType.getBoatTypeByName({
名称:boatTypeParsed
},功能成功(结果){
返回结果;
}).$承诺*/
//低于1.2.0的ngResource的代码
var deferred=$q.deferred(),promise=deferred.promise;
BoatType.getBoatTypeByName({
名称:boatTypeParsed
},deferred.resolve,deferred.reject);
回报承诺;
//调用服务方法,代码仍在运行,直到
//在不等待服务结果的情况下结束函数。
//然后执行parseURL中的promise.Then代码并
//搜索船在boatType未定义的情况下运行。
};
//具有对API的$resource调用的服务
应用工厂(‘船型’,
函数($resource,SERVER\u URL){
船型=
$resource('http://'+SERVER\u URL+'/:action'{
行动:"船型"
}, {
查询:{
方法:“GET”,
伊萨雷:是的
},
getBoatTypeByName:{
方法:“GET”,
参数:{
操作:“getBoatTypeByName”
},
艾萨瑞:错
}
});
返回船型;
}

)
我想我已经复制/粘贴了您的代码,但我遇到了以下错误:TypeError:无法读取此行中未定义的parseBoatType()的“then”属性。then(deferred.resolve);以防万一,我使用Angular 1.0.7是很重要的。@Rober,事实上,这个
$promise
属性是在本例中引入的,您必须像在第一个函数中一样使用promises。我会更新我的答案你能暗示一下你问题中的版本吗?更新了!;)你是我的英雄!你的答案有效!但是,为了锦上添花,如果不是只有一个parseBoatType函数,而是有两个不同的函数可以并行运行(第二个parseDestination),但在执行searchBoats之前,这两个函数都需要运行,那会怎么样?我认为它们可以嵌套,但有可能用$q.all并行完成吗?我将在这里创建一个新的帖子,扩展这个帖子的主题:我将链接回您以前的问题:以便其他人可以了解您问题的其他上下文。我很高兴你能解决你的问题。