为什么$http成功了't返回更新后的承诺

Why does $http success doesn't return the updated promise?

本文关键字:返回 更新 承诺 http 成功 为什么      更新时间:2023-09-26

所以我知道当对$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_。

在我看来,用一个胜过另一个是一个品味问题。