使用AngularJS 1.0.7中的$资源嵌套承诺

时间:2017-09-20 18:20:12

标签: javascript angularjs

我需要在AngularJS 1.0.7中使用参数运行函数" searchBoats(boatType)" 。此参数是另一个函数 parseBoatType 的结果,该函数正在运行调用带有$ resource 的API的服务。如果在带有promises的资源中返回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;           
  }
  )

1 个答案:

答案 0 :(得分:1)

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

首先从parseBoatTime函数返回一个承诺:

return BoatType.getBoatTypeByName({
    name: boatTypeParsed
  }, function success(result) {
    return result;
  }).$promise;

然后使用parseUrl资源中的承诺解析deferred BoatType

parseBoatType().then(deferred.resolve);

贝娄是我提到的纠正中你的问题的全部代码。

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

  parseBoatType().then(deferred.resolve);
};
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

  // Code for ngResource@^1.2.0
  /*return BoatType.getBoatTypeByName({
    name: boatTypeParsed
  }, function success(result) {
    return result;
  }).$promise;*/

  // Code for ngResource lower than 1.2.0
  var deferred = $q.defer(), promise = deferred.promise;

  BoatType.getBoatTypeByName({
    name: boatTypeParsed
  }, deferred.resolve, deferred.reject);

  return promise;

  // 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
app.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;
  }
)