Giacomo Masseroni Chiaro Giacomo Masseroni Chiaro - 1 month ago 6
AngularJS Question

angularjs: chain promises returns a promise instead of an object

I have a chain promises and I want to return an object instead of a promise. Instead I get a promise.
This is the function:

self.get = function(id) {
var parameters = [id];
var orders = {}
return DBA.queryAgent("SELECT * FROM Orders WHERE " + column_id + " = ?", parameters).then(function(result) {
return DBA.getById(result);
}).then(function(result) {
order = result;
return self.getProdutcs(id);
}).then(function(result) {
order.Products = result;
return order;
});
}


I expect that

service.get(1)


returns an object, I get a promise. Where am I wrong?

Thank you

Answer

From the angular docs https://docs.angularjs.org/api/ng/service/$q

calling the then method of a promise returns a new derived promise,

It is the way angular is written. Hence, you have to access it like this.

 service.get.then(function(result) {
    // whatever
  });