为什么$http成功了't返回更新后的承诺
Why does $http success doesn't return the updated promise?
所以我知道当对$http
结果调用success或error时,它会在$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;
}
我真的不知道该怎么看待这两种方法。。。是否有充分的理由针对这一限制使用它们?
感谢提供任何信息!
为了"意义":
$http.get(...).success(function(data) { ... }).error(function(data) { ... });
在这种情况下,每个函数都需要原始的$http
数据,因为这些回调彼此完全独立。
更多的编码风格,是否有充分的理由不替换这里的代码通过这个?
=>您将无法执行上面的代码,因为它会破坏这种独立性
事实上,这意味着只有当成功被触发=>完全没有意义时,错误才应该被触发。
您不能用常规的"低级"promise来实现这种"条件流",因为它与上面的一样明显。
事实上,你会这样做:
$http.get(...).then(function(data){ whenSucceed }, function(data) { whenError });
但您很容易注意到,这正是成功/错误代码在幕后所做的:
使用CCD_ 4进行拒绝并且使用then(data)
进行CCD_。
在我看来,用一个胜过另一个是一个品味问题。
相关文章:
- 如何进行sequialize更新以返回更新后的对象
- 变量dos'即使我可以返回更新后的值,也不会改变
- 函数返回后更新变量
- 将字符串转换为数组使用javascript更新一个值并返回一个字符串
- Ajax POST返回成功,但没有;t更新JSON文件
- 当视图在AngularJS中返回时,我如何获得异步服务调用来更新视图
- 如何在运行总计计算器时返回表单输入并更新 JavaScript 变量
- ASP:代码隐藏的字段变量在从更新面板中的服务器返回时丢失值
- 不可变,在映射内更新不返回正确的对象
- JavaScript:如何在每次更新一些值时返回一个函数
- RxJS:在每次返回并行http请求时更新客户端
- 从函数返回更新变量
- Backbone collection.create()不返回更新后的模型
- 如何从JSON返回更新我的下拉列表
- 如何在mongodb中更新时返回更新的对象
- Backbone.js集合.Create函数没有使用wait: true返回更新后的模型
- IIS上的AngularJS -浏览器缓存不返回更新后的页面模板
- 为什么$http成功了't返回更新后的承诺
- POST请求没有返回更新的数据
- findOneAndUpdate未在MongoDb中返回更新的文档