Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Order of invocation of Multiple Finalize callbacks #4138

Closed
Vikasg7 opened this issue Sep 15, 2018 · 1 comment
Closed

Order of invocation of Multiple Finalize callbacks #4138

Vikasg7 opened this issue Sep 15, 2018 · 1 comment

Comments

@Vikasg7
Copy link

Vikasg7 commented Sep 15, 2018

Bug Report

Current Behavior
Callbacks registered with finalizing operator are called in flipped order.

Reproduction
Link to codepen

const problem1 = pipe(
  from([1,2,3]),
  finalize(() => log("1")),
  map(x => x * 2),
  finalize(() => log("2"))
)

problem1.subscribe()

Current output

"2"
"1"

Expected output
This is what I expect. I would like to finalize previous things first before the things further down my chain.

"1"
"2"

Environment

  • Runtime: Node v9.4.0, Chrome v68.0.3440.106
  • RxJS version: 5+, 6+
@cartant
Copy link
Collaborator

cartant commented Sep 27, 2018

The order in which the finalize operators are called depends upon several factors.

In your snippet, they are called in 'reverse' order because the source and the composed operators complete synchronously.

In this example, they are not called in reverse order because the source observable completes before the observable within the mergeMap:

import { asyncScheduler, from, of } from "rxjs";
import { finalize, mergeMap } from "rxjs/operators";

const problem1 = from([1,2,3]).pipe(
  finalize(() => console.log("1")),
  mergeMap(x => of(x * 2, asyncScheduler)),
  finalize(() => console.log("2"))
);
problem1.subscribe();

Essentially, this behaviour is be design.

@cartant cartant closed this as completed Sep 27, 2018
@lock lock bot locked as resolved and limited conversation to collaborators Oct 27, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants