Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is there no onComplete() rxjs operator?

Tags:

rxjs

Why rxjs contains no onComplete operator, i.e., one that allows to do something when the source observable completes? The finalize operator works for both completion and error, while I need to only react to completion.

Yes, I know that I can use the onComplete callback of the subscribe() function, but this is something completely different. Sometimes you only want to do certain stuff on completion inside the rxjs pipeline, and not inside the subscribe call.

Am I missing something?

like image 878
Maksym Avatar asked Nov 16 '25 11:11

Maksym


1 Answers

The tap operator accepts three arguments (value, error, completion)

ons$.pipe(
    tap(null, null, () => console.log("Done")),
).subscribe() 

or alternatively an observer, so you can do this:

obs$.pipe(
    tap({ complete: () => console.log("Done") })
).subscribe()

See the operator docs for more: https://rxjs-dev.firebaseapp.com/api/operators/tap


I would recommend the observer syntax as this is the syntax that has been recommended by Ben Lesh in the past since it's more expressive.

like image 140
Ingo Bürk Avatar answered Nov 19 '25 09:11

Ingo Bürk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!