[英]AngularJS - Cannot get $q.reject and to work
Take a look at my code sample: 看看我的代码示例:
I've stripped it down to (what I believe are) the bare essentials, and I'm setting getting an error deferred.reject is not a function
我把它剥离到了(我相信的)最基本的东西,而且我设置得到一个错误
deferred.reject is not a function
angular
.module('myApp')
.service('myService', MyService);
MyService.$inject = ['$http', '$q'];
function MyService($http, $q) {
var self = this;
self.getResult = getResult;
function getResult(id) {
var deferred = $q.defer();
deferred = $http.get('my/api/method', { params: { id: id } })
.then(getResultCompleted)
.catch(getResultFailed);
function getResultCompleted(response) {
if (response.data.ID > 0) {
deferred.resolve(response.data);
} else {
deferred.reject(response);
}
}
function getResultFailed(response) {
deferred.reject(response);
}
return deferred.promise;
}
}
Note : I know I can get this to work by simply returning the result of the $http call, however for the purposes of understanding these promise objects, I want to see if I can get this working by declaring and returning
deferred
as shown above 注意 :我知道我可以通过简单地返回$ http调用的结果来实现这一点,但是为了理解这些promise对象,我想看看我是否可以通过声明并返回
deferred
来实现这个工作,如上所示
Thanks! 谢谢!
You are creating custom promise and returning it from your method, but mean while dealing with it you are overriding deferred
(custom promise object) with $http.get
, which also returns promise which has .then
& .catch
methods(Which doesn't have resolve
& reject
method). 您正在创建自定义的承诺,从你的方法还,但在处理它要覆盖的意思是
deferred
(自定义承诺对象)与$http.get
,这也返回承诺已.then
与.catch
方法(不有resolve
和reject
方法)。 Because of that assignment, you can't have access to .resolve
and .reject
from deferred
object. 由于该分配,您无法从
deferred
对象访问.resolve
和.reject
。
deferred = $http.get('my/api/method', { params: { id: id } })
should be only 应该是唯一的
$http.get('my/api/method', { params: { id: id } })
Since $http.get()
returns promise and success callback supports chaining API, you can define new deferred object inside of a callback and return it from there: 由于
$http.get()
返回promise并且成功回调支持链接API,因此您可以在回调中定义新的延迟对象并从那里返回它:
angular
.module('myApp')
.service('myService', MyService);
MyService.$inject = ['$http', '$q'];
function MyService($http, $q) {
var self = this;
self.getResult = getResult;
function getResultCompleted(response) { // Tip: Define callback outside of getResult in order to avoid creating new instance of it each time getResult is called
var deferred = $q.defer();
if (response.data.ID > 0) {
deferred.resolve(response.data);
} else {
deferred.reject(response);
}
return deferred.promise;
}
function getResult(id) {
return $http.get('my/api/method', { params: { id: id } })
.then(getResultCompleted);
}
}
Note, that there is no error callback any more (just because it is redundant in this case). 注意,不再有错误回调(仅因为在这种情况下它是多余的)。
In case request failed, the original rejected promise from $http.get()
will be returned, otherwise a brand new promise from success callback will be used for follow-up .then()
calls. 如果请求失败,将返回来自
$http.get()
的原始拒绝承诺,否则成功回调的全新承诺将用于后续.then()
调用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.