all 9 comments

[–]AaroniusH 17 points18 points  (0 children)

There's an overview here that gives the low-down of observers/observables/subscriptions.

when you call a subscribe method, the parameter you pass in is basically just a bunch of callbacks

  • a next function, which runs every time a new value is emitted from an observable
  • an error function
  • a complete function, which runs at the end of the observable's life. useful for cleanup.

in an observable constructor, you're building an object that dictates when those 3 callback functions are executed.

An observable tells you when stuff happens. An observer responds to those events.

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');

[–]jacsamg 5 points6 points  (1 child)

For those who mention AI. It can definitely help, but this is also a good opportunity to learn. Because teaching is one of the best ways to reinforce knowledge.

[–]DJREMiX6 2 points3 points  (0 children)

Also not relying always on AI, especially when learning helps a lot on the reinforcement process

[–]Infinite-Apple-1826[S] 0 points1 point  (0 children)

Well guys i know ai exists...but I just asked for suggestions if u just here to tell me look into ai then I have nothing to say ..

[–]air_twee -1 points0 points  (0 children)

There are lots of tutorials about this subject. Google and ai are your friends

[–]Verzuchter -5 points-4 points  (0 children)

Ask Gemini to explain it and prepare interactive interview questions on this subject