.getValue will synchronously get the immediate value out of the Observable while .next seems to set the next in the sequence value that you will hear while you are listening at length. Something already listening and not having had an update for a while should just get that update.
let cat = this.kittyObservable.getValue();
cat.lives--;
this.kittyObservable.next(cat);
This offers this on .next:
import { Observable } from 'rxjs';
const observable = new Observable(subscriber => {
subscriber.next(1);
subscriber.next(2);
subscriber.next(3);
setTimeout(() => {
subscriber.next(4);
subscriber.complete();
}, 1000);
});
console.log('just before subscribe');
observable.subscribe({
next(x) { console.log('got value ' + x); },
error(err) { console.error('something wrong occurred: ' + err); },
complete() { console.log('done'); }
});
console.log('just after subscribe');
It suggests that it spits back out:
- just before subscribe
- got value 1
- got value 2
- got value 3
- just after subscribe
- got value 4
- done
No comments:
Post a Comment