.then
返回新的承諾。
.success
返回原始承諾。另一個區別是,.success
獲取response.data
作爲輸入參數,而.then
獲取response
。
這是how it's implemented:
promise.success = function(fn) {
promise.then(function(response) {
fn(response.data, response.status, response.headers, config);
});
return promise;
};
你可以使用.success
(和.error
)來處理的$http
各自的結果,但沒有修改數據或拒絕承諾(含return $q.reject()
)給予的能力$http
承諾的最終消費者。
這裏是當一個人可以使用.success
VS /與.then
說明性的例子:
return {
getData: function(p1){
return $http.get(url, {p1: p1})
.success($log.log) // can use generic functions since there is
.error($log.error) // no requirement to return data
.then(function(response){
// here can massage the data into the output format
return response.data;
});
};
我建議您始終使用.then
,因爲它返回,並且不.success
和.error
處理新的承諾;這避免了API消費者使用$http
特定的.success
/.error
的機會因此無意地作出了在封面下使用$http
的假設。
[許諾只是回調的一個奇特符號](http://stackoverflow.com/q/22539815/1048572) – Bergi 2015-04-02 14:07:26