1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

What does "next" do inside of .subscribe()?

Discussão em 'Angular' iniciado por the_overflowing_stack, Outubro 1, 2024 às 10:22.

  1. I have seen three ways to "listen" for changes to a value via an observable / call APIs to fetch data from the backend.

    One of these ways has "next:" :

    this.MySubscription = this.myService.getStuff().subscribe({
    next: (data) => {
    <insert code to perform operations with "data">
    }, error: (err) => {
    console.error(err);
    // <insert code for what to do on failure>
    }
    });


    And on the Angular site https://angular.io/guide/observables I see this, with "next(" :

    // Call subscribe() to start listening for updates.
    const locationsSubscription = locations.subscribe({
    next(position) {
    console.log('Current Position: ', position);
    },
    error(msg) {
    console.log('Error Getting Location: ', msg);
    }
    });


    But I have been just doing it the "normal way", like this (with no "next"):

    this.MySubscription = this.myService.getStuff().subscribe((data: any) => {
    <insert code to perform operations with "data">
    }, error => {
    console.error(error);
    <insert code for what to do on failure>
    });


    Is there any functional difference between these three methods of subscribing? How does each method produce different results?

    Continue reading...

Compartilhe esta Página