javascript - 如何确保一个函数在调用另一个函数之前已经完成 - Promise Chaining Angular JS?

标签 javascript angularjs cordova ionic-framework

我构建了一组函数来检查是否有任何当前为 iOS 安排的通知,如果有,我想取消它们并重新安排它们。如果有预定的项目,下面的逻辑将返回,如果有将开始取消和重新安排。问题是,它们都显示成功并且控制台日志显示这一点,但重新安排功能似乎并没有重新安排它们。有没有办法确保在尝试重新安排之前已将它们移除?我唯一的猜测是我尝试在它完成删除之前重新安排时间。

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');

            });
      }

}

最佳答案

使用 finally 在 promise 完成后执行函数:

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();
    });
}

关于javascript - 如何确保一个函数在调用另一个函数之前已经完成 - Promise Chaining Angular JS?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41230350/

相关文章:

javascript - phonegap/web 数据库错误代码 解释?

android - Cordova Android 不允许向 fb-messenger 打开白名单 Intent

javascript - 制作以坐标为中心的 Canvas Sprite

javascript - 无法使用 $http 读取 json 文件

javascript - 如何重新分配函数参数

javascript - 无法使用 Angular.js 动态设置值

javascript - 服务器不读取 Cordova FileTransfer 选项参数

java - 安卓奇巧 : fileChooser + fileTransfer cordova plugin not working

javascript - 从字符串中提取数值并检查是否存在

javascript - 在 Vue JS 中处理登录 API token