Skip to content
This repository has been archived by the owner on Mar 12, 2024. It is now read-only.

Rxjs common usage scenarios #36

Open
reboottime opened this issue Dec 2, 2023 · 1 comment
Open

Rxjs common usage scenarios #36

reboottime opened this issue Dec 2, 2023 · 1 comment

Comments

@reboottime
Copy link
Owner

reboottime commented Dec 2, 2023

Overview

This article collections code snippets about some common usage scenarios in Rxjs.
Bellow are the list

@reboottime
Copy link
Owner Author

reboottime commented Dec 2, 2023

HTTP Request Related

  • Dependent Request: SwitchMap. In certain scenarios, subsequent HTTP requests depend on the data parameters obtained from previous requests. For example:

    • Obtain the current user's ID
    • Utilize the user ID to fetch the user's orders
  • Remove UI Loading Effect after API request completion: finalize. For example

    • Before:
      fetchUserOrders(userId: User['id']) {
        this.orderService
          .getOrdersByUserId(userId)
          .subscribe({
            next: (data) => {
              this.isLoadingOrders = false; 
              this.orders = data;
           },
            error: () => {
             this.isLoadingOrders = false; 
              this.notificationService.showError('Failed to fetch orders');
            },
          });
      }
      
    • After
      fetchUserOrders(userId: User['id']) {
       this.orderService
         .getOrdersByUserId(userId)
         .pipe(finalize(() => this.isLoadingOrders = false))
         .subscribe({
           next: (data) => {
             this.orders = data;
          },
           error: () => {
             this.notificationService.showError('Failed to fetch orders');
           },
         });
     }

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

1 participant