所以我知道,当对$http
结果调用成功或错误时,它将在$http
promise 上调用,然后将返回原始的而不是更新的。我真的不明白为什么?!
目前,您可以编写如下内容:
$http(config)
.success(function(data) { console.log(data); return 1; })
.then(function (response) {
var msg = 'Hey, I am the original response, not 1, ';
msg += 'and I can run even before success is completed. ';
msg += 'This is nearly fake chaining...';
console.log(msg);
});
更多的编码风格,是否有充分的理由不将这里的代码替换为这个?
// The util method has been put after the return
// just as the other $http internal methods
return decoratePromise(promise);
// Util method to add method 'success' and 'error'
// to a promise. This will spread the raw respons
function decoratePromise(p) {
p.success = function(fn) {
return decoratePromise(p.then(function(response) {
return fn(response.data, response.status, response.headers, config);
}));
};
p.error = function(fn) {
return decoratePromise(p.then(null, function(response) {
return fn(response.data, response.status, response.headers, config);
}));
};
return p;
}
我真的不知道如何看待这两种方法......是否有充分的理由使用它们来解决这个限制?
感谢您提供任何信息!