2

Turning off Internet Connection stops what AngularJS (Ionic v1) was doing

I have this code:

$ionicLoading.show({'template': 'loading'});

var promisesArray = [];

angular.forEach(photoCollection, function(photoPayload){
    var promise = sendPhotoInHttpRequest(photoPayload);
    promisesArray.push(promise);
});

$q.all(promisesArray).then(function(){
    doOtherStuff();
    $ionicLoading.hide();
});
// the end

function sendPhotoInHttpRequest(payload) {
    // used to force wait for counter to increase
    var defer = $q.defer();

    $http({
        'url': url,
        'method': 'POST',
        'data': JSON.stringify(payload)
    }).then(function(res){

        // whenever I cut off Internet connection 
        // the below code stop running 
        // but the photo already reached the server
        updateDatabaseSetPhotoAsSent(payload.id).then(function(res){

            increaseCounter(counterObj);
            defer.resolve('ok');

        }).catch(function(e){
            defer.reject(e);
        });

    }).catch(function(err){
        defer.reject(err);
    });

    return defer.promise;
}

In app.js I have two event listeners for online and offline to active and deactive one other service, don't know if it's related to this bug.

Whenever I cut off Internet connection while sending photos, the code that updates database rows stop running, although the photo already reached the web.

After some debugging, it seems that the first few $http requests doesn't reach then neither catch when I cut Internet connection, as if they were "cut in middle" and becomes a zombie. If I don't cut Internet, everything works ok.

I find it weird because everything besides the $http itself doesn't need Internet connection, so why does it stop?

How can I prevent that from stopping?

Edson Horacio Junior
  • 3,033
  • 2
  • 29
  • 50

0 Answers0