This article is a mirror article of machine translation, please click here to jump to the original article.

View: 4135|Reply: 1

[Angular] The RxJS finalize operator executes the logic after the Observable terminates

[Copy link]
Posted on 6/26/2022 6:49:57 PM | | |
Requirements: Angular projects, when clicking a button on a page, will send an HTTP request, because some interfaces are time-consuming, need to display the loading animation effect, when the HTTP request is completed, the loading animation will be closed.When the request is successful or an error occurs, you need to turn off the loading animation

Conventional writing

As follows:

That is, when the request succeeds and an error occurs, you need to call the close loading animation, is there an easier way? Similar to try-catch-finally in C#.

Use the finalize operator

What it does is try-catch-finalfinallyfunction, we only need to write the code once, and the logic will be executed regardless of success or failure.

The code is as follows:



How does finalize work? It basically adds a callback add(fn) to the disassembly of an observable object by subscribing. This guarantees that it will be called on error, completion, and unsubscription.

Note that the finalize operator is executed as soon as our Observable is terminated. This is important! For Angular HTTP, this works very well because the Angular HTTP service returns "done" once the request is complete.

(End)




Previous:Fiddler delayed interface response time and weak network test
Next:Extract all text from a PDF file using C# (supports .NET Core)
 Landlord| Posted on 9/18/2022 4:05:18 PM |
Subject topic subscription for Angular RxJS
https://www.itsvse.com/thread-9209-1-1.html
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com