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不是一个函数
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!
2 个解决方案
#1
3
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). Because of that assignment, you can't have access to .resolve
and .reject
from deferred
object.
您正在创建自定义承诺并从您的方法返回它,但在处理它时意味着您使用$ http.get覆盖延迟(自定义承诺对象),这也返回具有.then&.catch方法的promise(其中没有有解决和拒绝的方法)。由于该分配,您无法从延迟对象访问.resolve和.reject。
deferred = $http.get('my/api/method', { params: { id: id } })
should be only
应该是唯一的
$http.get('my/api/method', { params: { id: id } })
#2
1
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()调用。
#1
3
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). Because of that assignment, you can't have access to .resolve
and .reject
from deferred
object.
您正在创建自定义承诺并从您的方法返回它,但在处理它时意味着您使用$ http.get覆盖延迟(自定义承诺对象),这也返回具有.then&.catch方法的promise(其中没有有解决和拒绝的方法)。由于该分配,您无法从延迟对象访问.resolve和.reject。
deferred = $http.get('my/api/method', { params: { id: id } })
should be only
应该是唯一的
$http.get('my/api/method', { params: { id: id } })
#2
1
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()调用。