Skip to content

Commit 1dc09e9

Browse files
cartantbenlesh
authored andcommitted
fix(fromFetch): don't abort if fetch resolves (#4742)
* test(fetch): test expectations after complete * fix(fromFetch): don't abort if fetch resolves Closes #4739
1 parent cd5895d commit 1dc09e9

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

spec/observables/dom/fetch-spec.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -115,14 +115,19 @@ describe('fromFetch', () => {
115115
expect(response).to.equal(OK_RESPONSE);
116116
},
117117
error: done,
118-
complete: done,
118+
complete: () => {
119+
// Wait until the complete and the subsequent unsubscribe are finished
120+
// before testing these expectations:
121+
setTimeout(() => {
122+
expect(MockAbortController.created).to.equal(1);
123+
expect(mockFetch.calls.length).to.equal(1);
124+
expect(mockFetch.calls[0].input).to.equal('/foo');
125+
expect(mockFetch.calls[0].init.signal).not.to.be.undefined;
126+
expect(mockFetch.calls[0].init.signal.aborted).to.be.false;
127+
done();
128+
}, 0);
129+
}
119130
});
120-
121-
expect(MockAbortController.created).to.equal(1);
122-
expect(mockFetch.calls.length).to.equal(1);
123-
expect(mockFetch.calls[0].input).to.equal('/foo');
124-
expect(mockFetch.calls[0].init.signal).not.to.be.undefined;
125-
expect(mockFetch.calls[0].init.signal.aborted).to.be.false;
126131
});
127132

128133
it('should handle Response that is not `ok`', done => {

src/internal/observable/dom/fetch.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export function fromFetch(input: string | Request, init?: RequestInit): Observab
5555
const controller = new AbortController();
5656
const signal = controller.signal;
5757
let outerSignalHandler: () => void;
58+
let abortable = true;
5859
let unsubscribed = false;
5960

6061
if (init) {
@@ -73,9 +74,11 @@ export function fromFetch(input: string | Request, init?: RequestInit): Observab
7374
}
7475

7576
fetch(input, init).then(response => {
77+
abortable = false;
7678
subscriber.next(response);
7779
subscriber.complete();
7880
}).catch(err => {
81+
abortable = false;
7982
if (!unsubscribed) {
8083
// Only forward the error if it wasn't an abort.
8184
subscriber.error(err);
@@ -84,7 +87,9 @@ export function fromFetch(input: string | Request, init?: RequestInit): Observab
8487

8588
return () => {
8689
unsubscribed = true;
87-
controller.abort();
90+
if (abortable) {
91+
controller.abort();
92+
}
8893
};
8994
});
9095
}

0 commit comments

Comments
 (0)