How to ensure function execution before calling another - Angular JS promise chain?

I created a set of functions that check if there are any current notifications planned for iOS, and if there are any, I want to cancel them and reschedule them. The logic below will return if there are planned elements, and if they begin to cancel and reschedule. The problem is that they all show successful and console logs to show this, but the allocation function does not revise them. Is there any way to make sure they were deleted before trying to reschedule? My only guess is that I'm trying to reschedule before he has completed the deletion.

if (isIOS){


      var getScheduled = function() {
        $cordovaLocalNotification.getAllScheduled().then(function (scheduledItems) { 
          $scope.scheduledContainer = scheduledItems;
          $scope.scheduledItems = scheduledItems;
            if($scope.scheduledItems.length < 1) {
              console.log('no previously scheduled items.')
              return;
            } else {
              cancelAll();
              console.log("there are items here.")
            }

        })
      }
      getScheduled();

      // If there are notifications, cancel and reschedule.

      var cancelAll = function() {
        console.log('we made it to the cancel function');
        // Cancell All
        $cordovaLocalNotification.cancelAll().then(function (result) {
            console.log('They Cancelled');
            rescheduleAll();
        });
      }

      var rescheduleAll = function() {
        //Reschedule All
            $cordovaLocalNotification.schedule($scope.scheduledItems).then(function() {
              console.log('Successfully Scheduled');

            });
      }

}
+4
2

finally :

var cancelAll = function() {
    console.log('we made it to the cancel function');
    // Cancell All
    $cordovaLocalNotification.cancelAll().then(function (result) {
        // This is the success handler
    }, function(err) {
        // This is the error handler
    }).finally(function() {
        console.log('They Cancelled');
        rescheduleAll();
    });
}
+2

.success ?

var function1 = function() {
     //do something
}.success(function () {
    function2();
}

var function2 = function () {
    //do something
}
+1

Source: https://habr.com/ru/post/1664216/


All Articles