Search code examples
angularrxjsobservablesubscribe

How to use subscribe-next RxJS in Angular?


I am learning rxjs. I have an observable from an array. I want to put some fruit in function, using subscribe and next. I wrote but not right, may I help you. Thank you so much!

const fruits = from([
    "apple",
    "banana",
    "cherry"]);

fruits.subscribe(fruit => this.next(fruit));

Solution

  • const myObserver = {
      next: (item) => console.log('next: ', item),
      error: (err) => console.log('err: ', err),
      complete: () => console.log("Observable completed")
    };
    
    of([
      "apple",
      "banana",
      "cherry"
    ]).subscribe(myObserver);
    
    setTimeout(() => myObserver.next(["Apple"]) ,2000);
    

    Answer to Your exercise :D

    fruits.subscribe(fruit => toConveyorBelt(fruit));