javascript - AngularJS 1.0.7 中使用 $resources 嵌套 Promise

标签 javascript angularjs

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

最佳答案

您可以返回资源$promise来自BoatType资源在 parseBoatTime函数并使用 Promise 来解决 parseUrl推迟。

首先返回 parseBoatTime 的 promise 功能:

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

然后解析parseUrl deferred BoatType 的 promise 资源:

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

关于javascript - AngularJS 1.0.7 中使用 $resources 嵌套 Promise,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46329186/

相关文章:

angularjs - 使用 "resolve"单元测试 AngularJS 路由

javascript - $window.ga 在 AngularJS 事件中未定义?

javascript - 各种 jQuery 问题

javascript - 播放嵌入式 youtube 视频 JS

javascript jquery 自动删除文件

javascript - 使用 ionic1 (apk) 与使用 ocrad.js 的 Chrome(调试器)进行图像处理

html - JqWidgets - 将主题应用于 jqxgrid

javascript - 如何使用 Office Outlook Web 插件下载邮件文件?

javascript - jQuery:检查/取消检查 DOM 元素

Angularjs 过滤器匹配对象中的所有属性,即使我的表中只有一个属性