typescript - 然后里面的新承诺什么都不返回
问题描述
我正在使用 ionic 3 本机日历向设备日历添加新事件。当我可以检测到事件是否已经在日历中时,我希望有功能。
首先,我正在检查日历是否具有读/写权限,如果是,我想检查事件是否存在,如果不创建。问题可能出在checkIfEventExists
功能上。
checkCalendarUsePermission() {
this.calendar.hasReadWritePermission()
.then((isPermitted) => {
if (isPermitted) {
return true;
}
return this.calendar.requestReadWritePermission()
})
.then(() => {
return this.checkIfEventExists();
})
.then((result) => {
console.log('Result non exist', result);
if(result){
console.log(result);
this.createNewEvent()
}
})
.catch((err) => {
console.log('Error occured while getting permission', err)
this.appFunctionCtrl.presentToast('Error with accessing the write permission', 1500, 'bottom')
})
}
checkIfEventExists () {
console.log('data provided', this.data.title);
return this.calendar.findEvent(this.data.title)
.then((result: any) => {
console.log('found Event', result)
if(result){
return result;
}
return false;
})
.catch((err) => console.error('error with findEvent', err))
}
createNewEvent() {
const startDate = moment(this.data.dateFrom, 'DD.MM.YYYY').toDate();
const cleanDescription = this.removeHtmlTags(this.data.description);
const options = {
id: this.data.title
}
let endDate = moment(this.data.dateFrom, 'DD.MM.YYYY').toDate();
if (this.data.dateTo) {
endDate = moment(this.data.dateTo, 'DD.MM.YYYY').toDate();
}
this.calendar.createEventInteractivelyWithOptions(this.data.title, this.data.location, cleanDescription, startDate, endDate, options)
.then(() => {
if(options.id !== undefined) {
this.eventsIds.push(options.id)
}
console.log('event created successfully')
})
.catch((err) => console.error('Error occured', err))
}
解决方案
在第一个then块上,如果isPermitted为 true,则返回 true。这意味着承诺不会遵循其他链式函数。
当你返回一个承诺时,你只能链接另一个承诺。这里有一些片段来澄清这个想法。
checkCalendarUsePermission() {
this.calendar.hasReadWritePermission()
.then((isPermitted) => {
if (isPermitted) {
return new Promise((resolve, reject) => {
resolve(true);
});
}
return this.calendar.requestReadWritePermission()
})
.then(() => {
return this.checkIfEventExists();
})
.then((result) => {
console.log('Result non exist', result);
if(result){
console.log(result);
this.createNewEvent()
}
})
.catch((err) => {
console.log('Error occured while getting permission', err)
this.appFunctionCtrl.presentToast('Error with accessing the write permission', 1500, 'bottom')
})
}
推荐阅读
- linux - Open VPN - 将路由推送到私有子网
- reactjs - 如何在ReactJs中的组件的映射中获取元素之前的元素
- node.js - Nodejs Kinesis Client 失败,没有太多线索
- java - Spring MVC TomCat配置运行jsp和html页面
- php - Laravel 8:找不到类'App\Policies\Gate'
- javascript - 在 react 中使用关联数组填充 ant 设计表
- curl - 将 curl 中的 @filename 转换为 Google Apps 脚本
- javascript - 如何将 PHP 数组存储在 Javascript 数组中并显示?
- powershell - 使用 Powershell 实时监控日志文件并在发现任何匹配项时发送消息
- swift - 如何使用 Swift 编写简单的静态分析?