【发布时间】:2014-07-31 20:52:55
【问题描述】:
我有一个承诺链,它必须在链的中途调用$http,并将生成的承诺传递给下一个处理程序。问题是我的 .then() 成功函数似乎将返回的 $http 承诺包装在另一个承诺中,导致我的 http 承诺被视为有效负载。因此,当它在链中的下一个链接中解析时,我得到的是一个承诺,而不是 http 响应。
var statusPromise = Auth.getUserStatus();
var tokenPromise = statusPromise.then(function(status) {
if (status && status.accesstoken) {
return status.accesstoken;
} else {
return null;
}
});
var httpPromise = tokenPromise.then(function(accesstoken) {
console.log('access token', accesstoken);
var params = {
page: page,
limit: limit
};
if (accesstoken) {
params.accesstoken = accesstoken;
}
return $http.get(apiUrl + 'scenes/' + filter + '/', {
data: $.param(params),
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
});
});
var doMoreStuffPromise = httpPromise.then(function(response) {
console.log(response); // this logs the promise as the payload, not the http response
return response.data;
});
return doMoreStuffPromise;
如何在我的链中链接$http 承诺?
【问题讨论】:
标签: angularjs promise angular-services