fetch-abort-controller
v1.1.2
Published
'fetch-abort-controller' 是一个轻量级、易于使用的 npm 包,旨在通过封装 AbortController 简化网络请求的取消操作。它提供了一套直观的 API,允许开发者快速创建和管理 AbortController 实例,从而控制和取消 fetch API 的网络请求。无论是单个请求的取消还是批量处理,'fetch-abort-controller' 都能提供灵活而强大的支持。此外,它还支持移除不再需要的 controller 实例,帮助开发者有效管理资源,保持应用的高性能运行
Downloads
6
Readme
// 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);
})