首页 > 解决方案 > 然后里面的新承诺什么都不返回

问题描述

我正在使用 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))
    }


标签: typescriptasynchronousionic3

解决方案


在第一个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')
        })
}

推荐阅读