1.1.2 • Published 1 year ago
fetch-abort-controller v1.1.2
// example.js
import WebController from 'fetch-abort-controller';
const controllerId = 'controllerId'
const controllerInstance = WebController.getInstance();
const controller = controllerInstance.createController(controllerId);
setTimeout(() => {
controllerInstance.abort(controllerId)
}, 500)
fetch('http://localhost:3000/api/test', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
signal: controller.signal
})
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.then(data => {
console.log('Data received:', data);
}).catch(e => {
console.error('There has been a problem with your fetch operation:', e);
})