delay.promise在angularJS中返回相同的结果
我有一个从API获取数据的服务。当我试图调用此服务时。它以相同的值返回delay.promise在angularJS中返回相同的结果,angularjs,angular-promise,deferred,Angularjs,Angular Promise,Deferred,我有一个从API获取数据的服务。当我试图调用此服务时。它以相同的值返回 appName.service('FetchCustomerDate', ['$http', '$q', function($http, $q) { var self = this; self.getCustomerData = function(token,name) { var deferred = $q.defer(); return $http({
appName.service('FetchCustomerDate', ['$http', '$q', function($http, $q) {
var self = this;
self.getCustomerData = function(token,name) {
var deferred = $q.defer();
return $http({
method: 'GET',
url: ,
headers: {
"Authorization": token,
"x-xcmc-auth": ''
}
}).then(function(response) {
deferred.resolve(response);
return deferred.promise;
}, function(response) {
deferred.reject(response);
return deferred.promise;
});
};
}]);
我看到这里有点混乱。让我们试着把它弄清楚。如果要使用延迟对象,则需要稍微更改代码:
appName.service('FetchCustomerDate', ['$http', '$q', function ($http, $q) {
var self = this;
self.getCustomerData = function (token, name) {
var deferred = $q.defer();
$http({ // Do not return here, you need to return the deferred.promise
method: 'GET',
url: '...some URL here...',
headers: {
"Authorization": token,
"x-xcmc-auth": ''
}
}).then(function (response) {
deferred.resolve(response); // It's correct, you are resolving the deferred promise here.
// return deferred.promise; // You do not need to return the deferred.promise here.
}, function (response) {
deferred.reject(response); // It's correct, you are rejecting the deferred promise here.
// return deferred.promise; // You do not need to return the deferred.promise here.
});
return deferred.promise; // The function must return the deferred.promise
};
}]);
具体来说,函数getCustomerData
必须返回属于deferred
对象的承诺,并使用返回deferred.promise
。在then()。您不需要返回延迟的。promise
您可以改进代码。$http
服务返回一个承诺,由then
回调返回的值由then
方法包装在承诺中。知道了这一点,您可以删除对延迟对象的使用:
appName.service('FetchCustomerDate', ['$http', function ($http) {
var self = this;
self.getCustomerData = function (token, name) {
return $http({ // Here, you need to return the promise returned by $http. Than promise will contain the response returned inside "then" callbacks.
method: 'GET',
url: '...some URL here...',
headers: {
"Authorization": token,
"x-xcmc-auth": ''
}
}).then(function (response) {
return response; // Simply return the response, it will be wrapped in a resolved promise by "then()"
}, function (response) {
return response; // Simply return the response, it will be wrapped in a rejected promise by "then()"
});
};
}]);
如您所见,2then
回调只返回响应
对象,因此您可以省略它们:
appName.service('FetchCustomerDate', ['$http', function ($http) {
var self = this;
self.getCustomerData = function (token, name) {
return $http({ // Here, you need to return the promise returned by $http. Than promise will contain the response form the GET call
method: 'GET',
url: '...some URL here...',
headers: {
"Authorization": token,
"x-xcmc-auth": ''
}
});
};
}]);
通常,当您使用$http
服务获取数据时,您希望从响应中获取数据,并将其影响到$scope
,例如,或者以某种方式对其进行处理。你想干什么?请澄清你的问题
通常情况下,提取将如下所示:
appName.service('FetchCustomerDate', ['$http', '$q', function($http, $q) {
var self = this;
function notifyError(reason) {
console.error(reason);
}
self.getCustomerData = function(token,name) {
var deferred = $q.defer();
return $http({
method: 'GET',
url: ,
headers: {
"Authorization": token,
"x-xcmc-auth": ''
}
})
.then(function onSuccess(response) {
var cfg = response.data; // process data
})
.then(function onSuccess(response) {
// chained promises
})
.then(
function onSuccess(res) {
// ... this will trigger the chain reaction
deferred.resolve(res);
},
function onFailure(reason) {
notifyError(reason); // manage the error
deferred.reject(reason);
})
;
return deferred.promise;
}
}]);
“相同价值”是什么意思?现在,返回延迟。promise
行什么都不做;只需返回$http
承诺即可。不需要延期。您是否尝试过将return deferred.promise代码>行在.then()
的成功和失败函数之外?@MikeMcCaughan相同的值表示API响应与前一个API调用相同。@JFBeaulieu我尝试将“return deferred.promise;”置于.then()的成功和失败之外,但得到了相同的结果。