Le Thi Bich Dieu
Le Thi Bich Dieu

Reputation: 13

Return Observable<boolean> to a boolean value

I am using angular 7

I created an Observable value that I want true/false from it. Here is my code in comment-block.component.ts file

public maxSequencevalue: Sequence;

public hasNextComment(): Observable<boolean> {
    const latestSequence = this.commentListDataService.commentList.latestSequence();

    return this.commentAcquireService.getUnreadList(this.postId, -1)
        .pipe(
            map((commentList) => {
                this.maxSequencevalue = commentList.list[0].sequence;
                if(latestSequence.value < this.maxSequencevalue.value) {
                    return true;
                } else {
                    return false;
                }
            })
        );
}

How can I get a boolean, true/false value out of this.hasNextComment()?

Upvotes: 1

Views: 650

Answers (1)

Kamil Augustyniak
Kamil Augustyniak

Reputation: 429

If you want to get value from method which returns Observable<boolean>, you have to subscribe to this method.

For example:

this.hasNextComment().subscribe((val: boolean) => console.log(val)

You can write simpler your map method.

Instead of:

if(latestSequence.value < this.maxSequencevalue.value) {
  return true;
} else {
  return false;
}

use: return latestSequence.value < this.maxSequencevalue.value

Upvotes: 3

Related Questions