Javascript AngularJS-函数中对同一服务的多个调用
如果我在一个函数中对同一个服务进行多个调用,这是一个糟糕的代码设计吗?对服务的每次调用都返回一个值。有没有其他更好的方法来实现这一点 例如:Javascript AngularJS-函数中对同一服务的多个调用,javascript,angularjs,promise,Javascript,Angularjs,Promise,如果我在一个函数中对同一个服务进行多个调用,这是一个糟糕的代码设计吗?对服务的每次调用都返回一个值。有没有其他更好的方法来实现这一点 例如: $scope.getAdditionalInfo = () => { ExampleService .methodName($scope.parameter) .then(function (res) { //do things here
$scope.getAdditionalInfo = () => {
ExampleService
.methodName($scope.parameter)
.then(function (res) {
//do things here
}, function (err) {
alert(err.message);
console.log(err);
});
ExampleService
.methodName2($scope.parameter)
.then(function (res) {
//do things here
}, function (err) {
alert(err.message);
console.log(err);
});
ExampleService
.methodName3($scope.parameter)
.then(function (res) {
//do things here
}, function (err) {
alert(err.message);
console.log(err);
});
}
您可能希望将所有这些组合调用作为服务中的一个方法公开,并封装所有连接的调用,然后使用最后一个值返回到一个承诺之外。不,如果这些方法调用是该方法应该做的,那么这绝对没有错。唯一的缺点可能是函数体的大小,当它变得太长时(取决于“在这里做事情”),您可能希望将它分成三个较小的部分并调用它们。