I created a simple basic application which uses node-fetch
to send some HTTP(S)-Requests and uses p-limit
to reduce the amount of requests send at the same time. The problem is when the node-fetch
requests get aborted by an AbortController
they throw inside p-limit
so I can't handle the AbortError
.
I'm providing a stripdown version of the application which only contains the parts necessary to trigger the bug.
p-limit
import fetch, { FormData, File } from 'node-fetch';
import pLimit from 'p-limit';
async function sendRequest(abortController) {
try {
const formData = new FormData();
formData.set('file', new File(['Some text here..'], 'a.txt'));
await fetch('https://example.com', {
method: 'POST',
body: formData,
signal: abortController.signal,
});
} catch {
console.error("Something wen't wrong doing request.");
}
}
(async () => {
const limit = pLimit(10);
const abortController = new AbortController();
const jobs = [];
for (let i = 0; i < 10; i++) {
jobs.push(limit(() => sendRequest(abortController)));
}
abortController.abort();
await Promise.allSettled(jobs);
console.log('Done sending all requests.');
})();
which generates the following output:
...
Something wen't wrong doing request.
Something wen't wrong doing request.
Done sending all requests.
node:events:515
throw er; // Unhandled 'error' event
^
AbortError: The operation was aborted.
p-limit
import fetch, { FormData, File } from 'node-fetch';
async function sendRequest(abortController) {
try {
const formData = new FormData();
formData.set('file', new File(['Some text here..'], 'a.txt'));
await fetch('https://example.com', {
method: 'POST',
body: formData,
signal: abortController.signal,
});
} catch {
console.error("Something wen't wrong doing request.");
}
}
(async () => {
const abortController = new AbortController();
const jobs = [];
for (let i = 0; i < 10; i++) {
jobs.push(sendRequest(abortController));
}
abortController.abort();
await Promise.allSettled(jobs);
console.log('Done sending all requests.');
})();
which generates the following output:
...
Something wen't wrong doing request.
Something wen't wrong doing request.
Done sending all requests.
blocking all usage of p-limit - as I can't abort requests without crashing the entire application.
Best regards
UnlimitedBytes
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too