Search code examples
angulartypescriptasynchronoussubscription

finish parameter in angular 2 didn't work


I use subscribe to perform lines of codes after getting the result from API. Here are my codes

this.attRecService.getAgendaData(moment(this.viewDate).format('YYYY-MM')).subscribe(
        resp => {
            this.agendaData = resp;

            for (let item of resp) {
                if (item.schedule) {
                    for (let sched of item.schedule) {
                        this.events.push({
                            'start': new Date(moment(item.date).format('YYYY-MM-DD')),
                            'title': sched.title 
                        });
                    }
                }
            }
        },
        () => {
            console.log('display');
            this.displayClockDetail();
        }
    );

But in the part where I tried to console log the word display, it seems like it won' t get into that parameter. What is wrong?


Solution

  • This function

    () => {
        console.log('display');
        this.displayClockDetail();
    }
    

    will work only if the request is failed (some errors happen) and is not correctly handled before it.

    If you want a function to work when the observable is completed, you can pass as the third parameter to the subscribe function.

    .subscibe(() => { ok }, () => { error }, () => { completed })