简体   繁体   中英

forEach with $http.get how to handle when request stays pending and never returns

This is my first time using angular so very sorry about the bad codding.

Problem: I have an $http.get inside a forEach loop which gets data from a WebApi. This works fine until some of the calls stay pending and never get a return call. How can I write some logic to handle when a call stays pending ?

网络图片

Hourly.js

         $http.get('http://localhost:52332/api/HourlySummary/?' + 'div=' + divarg + '&line='+ i.line)                       
                .then(function (response) {
                         console.log('http then ', i.line);                             
                         count++;
                         if (i.line == 'S') {
                             storeS.valuesS = response.data;                                 
                             $scope.date = Date.now();
                         } else if (i.line == 'M') {
                             storeM.valuesM = response.data;                                 
                             $scope.date = Date.now();
                         } else if (i.line == 'L') {
                             storeL.valuesL = response.data;                                 
                             $scope.date = Date.now();                                 
                         }

                         if (count == 3) {
                             myLoop();
                         }
                     }, function (response) {
                         console.log("Somehting went wrong ", i.line);
                     });

You can set a timeout on the request:

$http.get('/urlpath/', { timeout: 10000 })   // wait 10 seconds

I think that puts a status code of -1 in the response.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM