B
B
Bogdan Bembenok2016-01-24 20:36:09
Angular
Bogdan Bembenok, 2016-01-24 20:36:09

Why does Resolve return an empty Promise?

Piece from Factory

order.getOrder = function(id) {
 		var deferred = $q.defer();
 		$http.get('/order-info/id' + id).success(function(order) {
 			deferred.resolve(order);
 		}).error(function(data, status, headers, config) {
 			deferred.reject(status);
 		});
 		console.log(deferred.promise);
 		return deferred.promise;
 	};

Here is the routing itself:
.state('order', {
      url: "/order/id{id}",
      views: {
        'content': {
          templateUrl: "/modules/order/order_details.html",
          controller: "orderCtrl",
          resolve: {
            orderService: 'orderService',
            order: ['$stateParams', 'orderService', function($stateParams, orderService) {
              var orderId = $stateParams.id;
              return orderService.getOrder(orderId);
            }]
          }
        },
      }
    })

What could be the gap?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman, 2016-01-28
@uaKorona

The problem is that you need to understand a little more about promises in angular
then always returns a new promise
Let's look at an example:

function asyncFunction() {  
  var deferred = $q.defer();  
  doSomethingAsync().then(function(res) {  
    res = asyncManipulate(res);
    deferred.resolve(res);
  }, function(err) {
    deferred.reject(err);
  });

  return deferred.promise; 
}

Here, a new promise, $q.defer(), is nonsensically created. The author of the code clearly did not know that then would return a promise. To improve the code, just return the result of then:
function asyncFunction() { 
  return doSomethingAsync().then(function(res) {  
    return asyncManipulate(res);
  }); 
}

Link to the full article
https://habrahabr.ru/post/221111/
see in the console what you get in the order - maybe it's empty?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question