【问题标题】:How to refresh data in a promise chain?如何刷新承诺链中的数据?
【发布时间】:2017-07-28 00:34:40
【问题描述】:

在下面的代码中,我想要实现的是每 10 秒调用一次return $http.get(newurl_3); 并刷新最后一个响应中的数据。我在不同的地方尝试了 setInterval 但无法使其工作。

它实际上是一个包含 5 个步骤的 Promise 链,总共有 100 多行代码。不确定将整个事情放在 setInterval 中是否是一个好习惯。

$http.get(url).then(function(data) {
    // playing with data and returning a new promise
    return $http.get(newurl_1); 
}).then(function (data) {
    return $http.get(newurl_2); 
}).then(function (data) {
    return $http.get(newurl_3); 
}).then(function (data) {
    return $http.get(newurl_4); // this call only needs to be refreshed. 
}).then(function (data) {
    // creating data array for UI. 
    // needs to be refreshed every 10 second to fetch updated data. 
    $scope.UI_presentation_Array = data... 
})

【问题讨论】:

  • 每隔 10 秒调用整个链 $http.get(url).then(function(data) { ... 等 - 因为 .then(function (data) { 中的 data 不能神奇地改变
  • @JaromandaX 这是 5 条链,总共 150 行代码。 :|
  • 放入一个函数中,在setInterval中调用该函数
  • @JaromandaX 只有最后 2 条链应该刷新。除了那个应用程序变得非常慢并且代码变得丑陋。
  • 您展示了一条链。所以说 2 或 5 条链是没有意义的

标签: javascript jquery angularjs ajax promise


【解决方案1】:

首先,考虑你的promise链可以写成如下:

var promise = $http.get(url).then(function(data) {
    // playing with data and returning a new promise
    return $http.get(newurl_1); 
}).then(function (data) {
    return $http.get(newurl_2); 
});

promise.then(function (data) {
    return $http.get(newurl_3); // this call only needs to be refreshed. 
}).then(function (data) {
    // creating data array for UI. 
    // needs to be refreshed every 10 second to fetch updated data. 
    $scope.UI_presentation_Array = data... 
});

请注意,中断发生在需要进行刷新的步骤之前。所以现在,promise 是过程的静态部分,promise.then()... 是您要重复的部分。

现在将重复的部分包装在function refresh() {...} 中,并从您的setInterval 中调用refresh,完整提供:

var promise = $http.get(url).then(function(data) {
    // playing with data and returning a new promise
    return $http.get(newurl_1); 
}).then(function (data) {
    return $http.get(newurl_2); 
});

function refresh() {
    return promise.then(function (data) {
        return $http.get(newurl_3); // this call only needs to be refreshed. 
    }).then(function (data) {
        // creating data array for UI. 
        // needs to be refreshed every 10 second to fetch updated data. 
        $scope.UI_presentation_Array = data... 
    });
}

var intervalRef = setInterval(refresh, 10000);

这可能就足够了,因为 10 秒的间隔相当长。除非获取非常滞后,否则您不会遭受“重叠” - 即获取周期在前一个完成之前开始的地方。

不过,为了完全安全,您应该考虑不要从 setInterval 调用 refresh(),而是在前一个周期完成时加上延迟。

【讨论】:

  • 谢谢,打破它似乎比包装整个东西更好。我认为将该部分分配给一个变量并干净地刷新一个只返回一个承诺的函数将是一个补充。顺便说一句,我还不确定如何处理速度。
  • 我发现只刷新了最后两个区块,而不是整个链。不知道为什么,但很有趣。
  • @DragonKnight,这个评论有点令人担忧。我认为只刷新最后两个块是练习的目标,整个答案都是在此基础上编写的。
  • 这是正确答案。只刷新最后两个块。它实际上也解决了我的另一个问题,我想多次使用第一个块的响应而不再次执行它。竖起大拇指。
  • 嘿,我松了一口气。祝你好运。
【解决方案2】:

如果需要整个链来加载数据,那么您可能别无选择,每次都必须以相同的方式调用它

function load() {    
    $http.get(url).then(function(data) {
        // playing with data and returning a promise
        return $http.get(newurl); 
    }).then(function (data) {
        // creating data array for UI. 
        // needs to be refreshed every 10 second to fetch updated data. 
        $scope.UI_presentation_Array = data... 
    })
}
setInterval( load, 10000);

【讨论】:

  • 只有最后 2 个请求需要重新加载。如果我这样做,那会变得非常丑陋和缓慢。
  • 我不确定我是否跟随。您在 $http.get(url)` 中调用 url 并在随后的 .then 中进行另一个 promise 调用,它会返回您需要的内容。其他承诺(在您提到的链中)是否在获得$scope.UI_presentation_Array 方面发挥作用?
  • 是的,它有一长串的弹性搜索请求来实现最后一个请求。最后一个表的数据只刷新。以前的都是静态的。
  • 那么除了将它们全部包含在函数中之外,我看不到任何其他方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-04-18
  • 2019-05-24
  • 2019-05-10
  • 1970-01-01
  • 2017-06-19
  • 2017-11-27
  • 2017-01-14
相关资源
最近更新 更多