简体   繁体   中英

RxJS Observables - Call function when any new value emitted

I have following the observable which sucesfully obtains JSON data from a firebase DB:

this.http.get('https://testdb.firebaseio.com/.json').map(res => res.json()).subscribe(result => {
  // do something with result
});

I want to call a function, foo() , inside the observable when any new value arrives after the first pull from the database. To be clear, I want foo() only when a value arrives "later".

For instance:

this.http.get('https://testdb.firebaseio.com/.json').map(res => res.json()).subscribe(result => {

  // Only called when a new value arrives in the stream later, not on the first pull when the stream is first activated.
  foo();

});

By later, I mean whenever the subscribed database is updated. I also don't need the actual value that was updated, I just want to trigger a function (which takes no parameters) when any new value arrives.

I've no experience with firebase but if you want to skip the very first value, then yourObservable.skip(1) will do it. Where yourObservable could be your data source ie:

this.http.get('https://testdb.firebaseio.com/.json')
.map(res => res.json())
.skip(1)
.subscribe((result) => foo();)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM