If you want to abort request manually, you should use AbortController and pass the signal to fatcher
import { fatcher } from 'fatcher';
const abortController = new AbortController();
fatcher('your-url', {
signal: abortController.signal,
}).catch(error => {
if (error instanceof DOMException && error.name === 'AbortError') {
// aborted.
return;
}
// other
});
abortController.abort();>$ npm install @fatcherjs/middleware-aborter<script src="https://cdn.jsdelivr.net/npm/fatcher/dist/fatcher.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@fatcherjs/middleware-aborter/dist/index.min.js"></script>
<script>
Fatcher.fatcher('xxx', {
middlewares: [FatcherMiddlewareAborter.timeout],
onTimeout: () => {},
timeout: 30000,
})
.then(response => {
console.log(response);
})
.catch(error => {
if (FatcherMiddlewareAborter.isTimeoutError(error)) {
// do somethings
return;
}
// do other thing
});
</script>declare module 'fatcher' {
interface FatcherOptions {
timeout?: number; // default 60 * 1000 (60s)
onTimeout?: () => void;
}
}import { fatcher } from 'fatcher';
import { timeout } from '@fatcherjs/middleware-aborter';
const response = await fatcher('xxx', {
middlewares: [timeout],
timeout: 30 * 1000,
onTimeout: () => {
console.log('timeout!');
},
});import { fatcher } from 'fatcher';
import { isTimeoutError, timeout } from '@fatcherjs/middleware-aborter';
fatcher('https://foo.bar', {
timeout: 30 * 1000,
middlewares: [timeout],
}).catch(error => {
if (isTimeoutError(error)) {
// do something..
return;
}
// other error
});