然后 return 里面的新承诺什么都没有
New promise inside of then return nothing
我正在使用 ionic 3 本机日历向设备日历添加新事件。当我可以检测事件是否已经在日历中时,我想要有功能。
首先,我要检查日历是否具有 read/write 权限,如果有,我想检查事件是否存在,如果不存在,则创建一个。问题可能在于 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 为真,则返回真。这意味着 promise 不会跟随其他链式函数。
您只能在返回承诺时链接另一个承诺。这里有一些片段来阐明这个想法。
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')
})
}
我正在使用 ionic 3 本机日历向设备日历添加新事件。当我可以检测事件是否已经在日历中时,我想要有功能。
首先,我要检查日历是否具有 read/write 权限,如果有,我想检查事件是否存在,如果不存在,则创建一个。问题可能在于 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 为真,则返回真。这意味着 promise 不会跟随其他链式函数。
您只能在返回承诺时链接另一个承诺。这里有一些片段来阐明这个想法。
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')
})
}