Verthon
Verthon

Reputation: 3247

New promise inside of then return nothing

I'm using the ionic 3 native calendar for adding new events to device calendar. I would like to have feature when I can detect whether event is already in calendar or not.

First I'm checking if calendar has read/write permissions if so I would like to check if event exist, if not create one. Problem probably lies in checkIfEventExists function.

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


Upvotes: 0

Views: 102

Answers (1)

Diego Ferreira
Diego Ferreira

Reputation: 70

On the first then block, if isPermitted is true, you are returning true. Which means that the promise won't follow the other chained functions.

You only can chain another promise when you're returning a promise. Here is some snippet to clarify the idea.

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

Upvotes: 1

Related Questions