Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions example/__tests__/nitrofetch.harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,30 @@ describe('NitroFetch - AbortController', () => {
expect(elapsed).toBeLessThan(5000);
});

it('abort after the response is buffered still rejects', async () => {
const controller = new AbortController();
const pending = nitroFetch(`${BASE}/get`, { signal: controller.signal });

// Block the JS thread so native finishes while the continuation cannot run,
// then abort inside that window — cancel() is already a no-op by then.
const until = Date.now() + 1500;
let spins = 0;
while (Date.now() < until) {
spins += 1;
}
expect(spins).toBeGreaterThan(0);
controller.abort();

let threw = false;
try {
await pending;
} catch (e: any) {
threw = true;
expect(e.name).toBe('AbortError');
}
expect(threw).toBe(true);
});

it('normal fetch with signal (not aborted) succeeds', async () => {
const controller = new AbortController();
const res = await nitroFetch(`${BASE}/get`, {
Expand Down
5 changes: 5 additions & 0 deletions packages/react-native-nitro-fetch/src/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,11 @@ async function nitroFetchRaw(

try {
const res: NitroResponseNative = await client.request(req);
// Native cancellation is best-effort — cancel() is a no-op once the
// response is buffered, so an aborted request can still resolve here.
if (signal?.aborted) {
throw createAbortError();
}
if (inspectorId) {
NetworkInspector._recordEnd(
inspectorId,
Expand Down
Loading