我构建了一组函数来检查是否有任何当前为 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/