javascript – $q.defer()不使用Angular服务
作者:互联网
我是angular的$q的新手,我正在尝试构建一个API调用服务并将其返回给控制器.
问题:无论我如何格式化它,服务都会在它到达$http.get(…)之前返回
服务:
// methods: query new, get existing
makeRequest: function(url, following) {
// create promise
var deferred = $q.defer();
$http.get(url, {
params: {
"following": JSON.stringify(following)
}
})
.then(function(res) {
console.log(res);
deferred.resolve(res.data);
});
return deferred.promise;
},
getFeed: function(user) {
console.log('>> userService[getUser]: retrieving user...');
if (!this.activities) {
// Request has not been made, setting user profile.
console.log('>> userService[getUser]: No user stored, making request...');
var following = this.compileArray(user);
console.log(following);
this.activities = this.makeRequest('api/network/activities', following);
};
// Return the myObject stored on the service
return this.activities;
}
调节器
$scope.recentActivity = activityService.getFeed(profile);
// also tried
activityService.getFeed(profile).then(function (res) {
$scope.recentActivity = res;
console.log(res);
});
编辑:上午9:40 05/06/2015
如果可能的话,我想从服务中检索控制器中的活动列表,就像它是新的一样(使用.then).那是可能/不好的做法吗?
getFeed: function(user) {
if (!this.activities) {
...
} else {
feedPromise = $q(function(resolve){ resolve(this.activities) });
console.log(feedPromise);
// returns: {$$state: Object, then: function, catch: function, finally: function}
feedPromise.then(function(res) {
console.log(res);
// returns: undefined
});
console.log(that.activities);
// Works, returns list of activities.
}
解决方法:
除非您将带有回调的基于非承诺的API转换为基于承诺的API,否则无需使用$q.defer(即使这样,建议使用$q(函数(解析,拒绝){. ..})).
$http已经返回一个承诺 – 只返回那个(或链接的.then承诺);
var httpResponsePromise = $http.get(url); // returns a promise
var actualDataPromise = httpResponsePromise.then(function(resp){ return resp.data; });
return actualDataPromise;
或更短(和典型):
return $http.get(url).then(function(response){
return response.data;
});
其次,承诺返回API会立即同步返回承诺 – 而不是结果.你需要一个.then才能得到结果.
最后,一旦API是异步的,它应始终是异步的 – 不要将其转换为同步或有时同步API.因此,无论在哪里,一直到数据的最终接收者,您都需要使用.then处理程序.
因此,您的服务API可以非常简单:
makeRequest: function(url, following){
return $http.get(url, {params: { "following": following }})
.then(function(response){
return response.data;
});
},
getFeed: function(user) {
var that = this;
var feedPromise;
if (!that.activities) {
var following = this.compileArray(user);
feedPromise = this.makeRequest('api/network/activities', following)
.then(function(activities){
that.activities = activities;
return activities;
});
} else {
feedPromise = $q(function(resolve){ resolve(that.activities); });
// or you could have cached the old feedPromise and returned that
}
return feedPromise;
}
控制器中的用法与任何其他基于promise的API一样:
activityService.getFeed(profile)
.then(function(activities) {
$scope.recentActivity = activities;
});
标签:javascript,angularjs,q 来源: https://codeday.me/bug/20190623/1274401.html