javascript - How to make multiple http requests in my case -


i'm trying chain promise angular $resource.

i have following factory:

angular.module('myapp').factory('product', ['$resource', function ($resource) {     return $resource(         '/api/product/:name',         { name: '@name' },         { 'getsub': {                 url: '/api/product/getsub/:name',                 method: 'get'}          }     ); }]); 

i make multiple queries using product factory such:

product.query({'name': name}, function(product) {      product.getsub({'name': product.name}, function(subitem) {          product.getsub({'name':subitem.name}, function(childitem) {              //do stuff child item          })      }) }) 

is there better way this? feel nesting these calls not best practice.

you can chain promises together!

product.query({'name': name}).$promise .then(function(product){   return product.getsub({'name': product.name}).$promise; }) .then(function(subitem){   return product.getsub({'name': subitem.name}).$promise; }) .then(function(item){   // etc }) 

Comments