diff --git a/spec/operators/finalize-spec.ts b/spec/operators/finalize-spec.ts index 4a1011b76c..fffdfda5d7 100644 --- a/spec/operators/finalize-spec.ts +++ b/spec/operators/finalize-spec.ts @@ -193,4 +193,16 @@ describe('finalize operator', () => { subscription.unsubscribe(); expect(order).to.deep.equal(['teardown', 'finalize']); }); + + it('should finalize after the teardown with synchronous completion', () => { + const order: string[] = []; + const source = new Observable(subscriber => { + subscriber.complete(); + return () => order.push('teardown'); + }); + source.pipe( + finalize(() => order.push('finalize')) + ).subscribe(); + expect(order).to.deep.equal(['teardown', 'finalize']); + }); }); diff --git a/src/internal/operators/finalize.ts b/src/internal/operators/finalize.ts index 1e874fd3bf..11febe684d 100644 --- a/src/internal/operators/finalize.ts +++ b/src/internal/operators/finalize.ts @@ -68,25 +68,8 @@ class FinallyOperator implements Operator { } call(subscriber: Subscriber, source: any): TeardownLogic { - return subscribeWith(source, new FinallySubscriber(subscriber, this.callback)); - } -} - -/** - * We need this JSDoc comment for affecting ESDoc. - * @ignore - * @extends {Ignored} - */ -class FinallySubscriber extends Subscriber { - constructor(destination: Subscriber, private callback?: () => void) { - super(destination); - } - unsubscribe() { - super.unsubscribe(); - const { callback } = this; - if (callback) { - callback(); - this.callback = undefined; - } + const subscription = subscribeWith(source, subscriber); + subscription.add(this.callback); + return subscription; } }