如何同时检查多个响应
how to check multiple responses simultanously
$scope.placeOrder = function() {
var callbackBalance = apiService.updateBalance($scope.order);
callbackBalance.then(function(data) {
if(data.data.success) {
var callback = apiService.createOrder($scope.order);
callback.then(function(data){
if(data.data.success)
{
localStorageService.cookie.clearAll();
alert("Order Placed Successfully");
$state.go("createOrder");
}
else
alert('Sorry! Cannot place order');
});
}
else
{
alert('Cannot update company balance')
}
});
这是一个为公司下订单并根据订单更新其余额的代码 total.The 代码工作正常但根据此代码首先调用余额 API 并且一旦它response is success 命令 API 将被调用,它的响应将是 checked.But,我们如何检查两者是否都成功然后只更新数据库的余额和 order.Right 现在一个案例可以是余额为公司更新,但由于某种原因没有下订单。
我正在使用 MEAN 堆栈进行开发。
您可以使用来自 angular
的 $q 服务
A service that helps you run functions asynchronously, and use their
return values (or exceptions) when they are done processing. You can
create a request object like this
您可以创建请求数组并将其传递给 $a.all 函数。
var request = [apiService.updateBalance($scope.order), apiService.createOrder($scope.order)]
并使用$q.all函数同时获取提供的请求
$q.all(request).then(function (response) {
}, function (error) {
});
即会同时获取请求数据。确保添加 $q 作为依赖项。
$scope.placeOrder = function() {
var callbackBalance = apiService.updateBalance($scope.order);
callbackBalance.then(function(data) {
if(data.data.success) {
var callback = apiService.createOrder($scope.order);
callback.then(function(data){
if(data.data.success)
{
localStorageService.cookie.clearAll();
alert("Order Placed Successfully");
$state.go("createOrder");
}
else
alert('Sorry! Cannot place order');
});
}
else
{
alert('Cannot update company balance')
}
});
这是一个为公司下订单并根据订单更新其余额的代码 total.The 代码工作正常但根据此代码首先调用余额 API 并且一旦它response is success 命令 API 将被调用,它的响应将是 checked.But,我们如何检查两者是否都成功然后只更新数据库的余额和 order.Right 现在一个案例可以是余额为公司更新,但由于某种原因没有下订单。 我正在使用 MEAN 堆栈进行开发。
您可以使用来自 angular
的 $q 服务A service that helps you run functions asynchronously, and use their return values (or exceptions) when they are done processing. You can create a request object like this
您可以创建请求数组并将其传递给 $a.all 函数。
var request = [apiService.updateBalance($scope.order), apiService.createOrder($scope.order)]
并使用$q.all函数同时获取提供的请求
$q.all(request).then(function (response) {
}, function (error) {
});
即会同时获取请求数据。确保添加 $q 作为依赖项。