javascript - How to refresh data in a promise chain? -
in following code, want achieve make return $http.get(newurl_3);
call every 10 second , refresh data in last response. tried setinterval in different places not make work.
it chains of promises in 5 steps , in total more 100+ line of code. not sure if putting whole thing in setinterval practice.
$http.get(url).then(function(data) { // playing data , returning 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); // call needs refreshed. }).then(function (data) { // creating data array ui. // needs refreshed every 10 second fetch updated data. $scope.ui_presentation_array = data... })
first, consider promise chain can written, follows:
var promise = $http.get(url).then(function(data) { // playing data , returning new promise return $http.get(newurl_1); }).then(function (data) { return $http.get(newurl_2); }); promise.then(function (data) { return $http.get(newurl_3); // call needs refreshed. }).then(function (data) { // creating data array ui. // needs refreshed every 10 second fetch updated data. $scope.ui_presentation_array = data... });
note break, comes before step @ refresh needs occur. now, promise
static part of process , promise.then()...
part want repeat.
now wrap repeated part in function refresh() {...}
, call refresh
setinterval
, giving, in full :
var promise = $http.get(url).then(function(data) { // playing data , returning 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); // call needs refreshed. }).then(function (data) { // creating data array ui. // needs refreshed every 10 second fetch updated data. $scope.ui_presentation_array = data... }); } var intervalref = setinterval(refresh, 10000);
that suffice due long interval of 10s. unless gets laggy, won't suffer "overlap" - ie cycle starts before preceding 1 has completed.
for full safety though, should consider calling refresh()
not setinterval
on completion of previous cycle plus delay.
Comments
Post a Comment