[英]How to make multiple http requests in my case
I'm trying to chain a promise with Angular $resource. 我正试图用Angular $资源链接一个承诺。
I have the 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 my Product factory as such: 我使用我的产品工厂进行多次查询:
Product.query({'name': name}, function(product) {
Product.getSub({'name': product.name}, function(subItem) {
Product.getSub({'name':subItem.name}, function(childItem) {
//do stuff with child item
})
})
})
Is there a better way to do this? 有一个更好的方法吗? I feel like nesting all these calls is not a best practice.
我觉得嵌套所有这些电话不是最好的做法。
You can chain the 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
})
you can use waterfall of async library or implement it yourself. 您可以使用异步库的瀑布或自己实现它。
here's sample code for your case. 这是您案例的示例代码。
async.waterfall([
function(callback) {
Product.query({'name': name}, function(product) {
callback(null, product);
})
},
function(product, callback) {
Product.getSub({'name': product.name}, function(subItem) {
callback(null, product, subItem);
})
},
function(product, subItem, callback) {
Product.getSub({'name':subItem.name}, function(childItem) {
var result = {};
result.childItem = childItem;
result.subItem = subItem;
result.product = product;
callback(null, result);
})
}
], function (err, result) {
//do stuff with result
});
A useful solution maybe use $q library 一个有用的解决方案可能使用$ q库
https://docs.angularjs.org/api/ng/service/ $q https://docs.angularjs.org/api/ng/service/ $ q
You can use the method $q.all() to send a lot of request and manage only one callback then() or make $q.defer() and resolve por reject your oun promises. 您可以使用方法$ q.all()发送大量请求并只管理一个回调then()或make $ q.defer()并解决拒绝您的承诺。
I currently answer this question from a mobile device and i can't make an example. 我目前从移动设备上回答这个问题,我不能举个例子。 Sorry about that.
对于那个很抱歉。 If when I get home that mistake trains still try to help
如果当我回到家时,错误列车仍然试图提供帮助
If you want the requests to be done one after another (like you have in your example) you could do a recursive function like this: 如果您希望一个接一个地完成请求(就像您在示例中所做的那样),您可以执行这样的递归函数:
in this example i want to upload a couple of images (calling a http route): 在这个例子中,我想上传几个图像(调用http路由):
$scope.uploadImageLayout = function (currentIndex, numberOfItems) {
if (currentIndex === numberOfItems) {
// in here you could do some last code after everything is done
} else {
Upload.upload({
url: 'localhost:3000/ficheiros',
file: $scope.imagesToUpload[$scope.auxIndex].file
}).success(function (data, status, headers, config) {
if ($scope.auxIndex < numberOfItems) {
$scope.uploadImageLayout(currentIndex + 1, numberOfItems);
}
});
}
};
and the first time you call just do this: 你第一次打电话就是这样做的:
$scope.uploadImageLayout(0, $scope.imagesToUpload.length);
in you case its the same but instead of the Upload.upload request you should have your request and catch the callback function(s). 在你的情况下它是相同的但不是Upload.upload请求你应该有你的请求并捕获回调函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.