diff --git a/spec/observables/dom/fetch-spec.ts b/spec/observables/dom/fetch-spec.ts index 93923c7914..3722705abc 100644 --- a/spec/observables/dom/fetch-spec.ts +++ b/spec/observables/dom/fetch-spec.ts @@ -115,14 +115,19 @@ describe('fromFetch', () => { expect(response).to.equal(OK_RESPONSE); }, error: done, - complete: done, + complete: () => { + // Wait until the complete and the subsequent unsubscribe are finished + // before testing these expectations: + setTimeout(() => { + expect(MockAbortController.created).to.equal(1); + expect(mockFetch.calls.length).to.equal(1); + expect(mockFetch.calls[0].input).to.equal('/foo'); + expect(mockFetch.calls[0].init.signal).not.to.be.undefined; + expect(mockFetch.calls[0].init.signal.aborted).to.be.false; + done(); + }, 0); + } }); - - expect(MockAbortController.created).to.equal(1); - expect(mockFetch.calls.length).to.equal(1); - expect(mockFetch.calls[0].input).to.equal('/foo'); - expect(mockFetch.calls[0].init.signal).not.to.be.undefined; - expect(mockFetch.calls[0].init.signal.aborted).to.be.false; }); it('should handle Response that is not `ok`', done => { diff --git a/src/internal/observable/dom/fetch.ts b/src/internal/observable/dom/fetch.ts index 3513ea0803..22953d9767 100644 --- a/src/internal/observable/dom/fetch.ts +++ b/src/internal/observable/dom/fetch.ts @@ -55,6 +55,7 @@ export function fromFetch(input: string | Request, init?: RequestInit): Observab const controller = new AbortController(); const signal = controller.signal; let outerSignalHandler: () => void; + let abortable = true; let unsubscribed = false; if (init) { @@ -73,9 +74,11 @@ export function fromFetch(input: string | Request, init?: RequestInit): Observab } fetch(input, init).then(response => { + abortable = false; subscriber.next(response); subscriber.complete(); }).catch(err => { + abortable = false; if (!unsubscribed) { // Only forward the error if it wasn't an abort. subscriber.error(err); @@ -84,7 +87,9 @@ export function fromFetch(input: string | Request, init?: RequestInit): Observab return () => { unsubscribed = true; - controller.abort(); + if (abortable) { + controller.abort(); + } }; }); }