What is the difference between finally and finaliz

2020-07-06 03:22发布

问题:

I am using RxJS and I can see there are 2 function in RxJS 5.5.2 available. Is the .finally will be removed and it will be placed in side pipe() from RxJS 6.0.0 on wards is the reason or there are any other changes?

Are they both same and now final call is inside pipe()?

Or they have any notable difference?

finalize

method()
   .pipe(
     finalize(() => {
      // do some operation
     })
   )

finally

method()
  .finally(() => {
    // do your operation
  })

回答1:

Both are same functionality wise both does same operation of calling once observable is completed but difference is which version of rxjs you are using

Before v5.5 it is been called as finally

From v5.5 it is renamed to finalize(due to keyword restriction), because of the introduction to pipeable Operators which helps better tree shaking. For more info please check this link



回答2:

They're very similar, but there is one notable difference that I can think of. finalize (by virtue of being an operator) doesn't have to be at the end of the pipe. This may seem insignificant, but depending on what operators are behind it in the chain there could be differences.

Consider for example a chain from a fetch that uses shareReplay to be shared between multiple components without retriggering an HTTP call. If you place finalize before shareReplay it will only be called once (because the fetch will only complete once), but if you place it after shareReplay, it will be called once per subscriber as shareReplay will create an Observable with its own finalize for each subscriber.



标签: angular rxjs