manidos
manidos

Reputation: 3464

How to detect change in promise value?

I want to poll some function that returns promises and detect change in resolved values. I need to somehow add interval operator here.

const observer = (newValue) => {
  console.log('Change detected', newValue);
}

Observable.fromPromise(getValue())
  .distinctUntilChanged((oldValue, newValue) => oldValue == newValue)
  .subscribe(observer);

Upvotes: 1

Views: 416

Answers (1)

Olaf Horstmann
Olaf Horstmann

Reputation: 16882

A typical case for switchMap

Observable.interval(1000)
  .switchMap(() => Observable.fromPromise(getValue())
  .distinctUntilChanged((oldValue, newValue) => oldValue == newValue)
  .subscribe(observer);

Upvotes: 3

Related Questions