stackoverflow.com/questions/46348382/...

ужно запустить функцию"SearchBoats (boatType)" в AngularJS 1.0.7 с параметром. Этот параметр является результатом другой функцииparseBoatType который запускает службу, которая вызывает API с $ ресурсом, Как я могу запустить searchBoats, когда boatType возвращается в ресурс с обещаниями?

Вот что я попробовал:

    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;           
  }
  )

Ответы на вопрос(1)

Ваш ответ на вопрос