当前位置: 首页 > news >正文

XHR / Fetch / Axios 请求的取消请求与请求重试

XHR / Fetch / Axios 请求的取消请求请求重试是前端性能优化与稳定性处理的重点,也是面试高频内容。下面是这三种方式的详解封装方案(可直接复用)。


✅ 一、Axios 取消请求与请求重试封装

1. 安装依赖(可选,用于扩展)

npm install axios

2. 封装 cancelToken 与 retry 的 axios 请求模块

// axiosRequest.js
import axios from 'axios'const pendingMap = new Map()// 生成唯一 key(用于标记请求)
function getRequestKey(config) {const { method, url, params, data } = configreturn [method, url, JSON.stringify(params), JSON.stringify(data)].join('&')
}// 添加请求到 pendingMap
function addPending(config) {const key = getRequestKey(config)config.cancelToken = new axios.CancelToken(cancel => {if (!pendingMap.has(key)) {pendingMap.set(key, cancel)}})
}// 移除请求
function removePending(config) {const key = getRequestKey(config)if (pendingMap.has(key)) {const cancel = pendingMap.get(key)cancel && cancel()pendingMap.delete(key)}
}// 重试机制封装
function retryAdapterEnhancer(adapter, options = {}) {const { retries = 3, delay = 1000 } = optionsreturn async config => {let retryCount = 0const request = async () => {try {return await adapter(config)} catch (err) {if (retryCount < retries) {retryCount++await new Promise(res => setTimeout(res, delay))return request()} else {return Promise.reject(err)}}}return request()}
}// 创建实例
const axiosInstance = axios.create({timeout: 5000,adapter: retryAdapterEnhancer(axios.defaults.adapter, { retries: 2, delay: 1000 })
})// 请求拦截器
axiosInstance.interceptors.request.use(config => {removePending(config)addPending(config)return config
})// 响应拦截器
axiosInstance.interceptors.response.use(response => {removePending(response.config)return response},error => {if (axios.isCancel(error)) {console.warn('Request canceled:', error.message)}return Promise.reject(error)}
)export default axiosInstance

✅ 使用示例:

import request from './axiosRequest'request.get('/api/data', { params: { id: 1 } }).then(res => console.log(res)).catch(err => console.error(err))

✅ 二、Fetch 封装(支持取消请求 & 重试)

1. 使用 AbortController 封装取消与重试

// fetchRequest.js
export function fetchWithRetry(url, options = {}, retries = 3, delay = 1000) {const controller = new AbortController()options.signal = controller.signalconst fetchData = (retryCount = 0) => {return fetch(url, options).then(res => {if (!res.ok) throw new Error('Network response was not ok')return res}).catch(err => {if (retryCount < retries && err.name !== 'AbortError') {return new Promise(resolve =>setTimeout(() => resolve(fetchData(retryCount + 1)), delay))}throw err})}return {promise: fetchData(),cancel: () => controller.abort()}
}

✅ 使用示例:

const { promise, cancel } = fetchWithRetry('/api/data', {}, 2, 1000)promise.then(res => res.json()).then(data => console.log(data)).catch(err => console.error(err))// 调用 cancel() 可随时取消请求

✅ 三、XHR 原生封装(带取消 & 重试)

// xhrRequest.js
export function xhrWithRetry({ url, method = 'GET', data = null, retries = 3, delay = 1000 }) {let xhr = nullconst send = (retryCount = 0, resolve, reject) => {xhr = new XMLHttpRequest()xhr.open(method, url, true)xhr.onreadystatechange = () => {if (xhr.readyState === 4) {if (xhr.status >= 200 && xhr.status < 300) {resolve(xhr.responseText)} else if (retryCount < retries) {setTimeout(() => send(retryCount + 1, resolve, reject), delay)} else {reject(new Error(`Request failed: ${xhr.status}`))}}}xhr.onerror = () => reject(new Error('Network Error'))xhr.send(data)}const promise = new Promise((resolve, reject) => send(0, resolve, reject))return {promise,cancel: () => xhr && xhr.abort()}
}

✅ 使用示例:

const { promise, cancel } = xhrWithRetry({ url: '/api/data' })promise.then(res => console.log(res)).catch(err => console.error(err))// 可随时调用取消
cancel()

✅ 总结对比

特性/方式取消支持重试支持易用性推荐场景
Axios✔️ CancelToken✔️ 自定义 adapter✅ 最佳Vue/React 项目
Fetch✔️ AbortController✔️ 手动封装✅ 清晰原生 / SSR 项目
XHR✔️ abort()✔️ 手动封装❌ 复杂老旧兼容需求

如果你想把这三套方案整合成一个通用库或 TypeScript 模块,我也可以帮你封装成统一接口版本。是否需要我继续处理这部分?

相关文章:

  • 箭头函数 vs 普通函数:区别与使用场景
  • 基于Pandas数据分析的设备巡检计划生成算法设计及实现
  • AI驱动游戏开发:Unity与ML-Agents结合
  • 数据库优化实战分享技术文章大纲
  • mysql跨库关联查询及视图创建
  • 国内头部的UWB企业介绍之品铂科技
  • Selenium 查找页面元素的方式
  • 企业培训学习考试系统源码 ThinkPHP框架+Uniapp支持多终端适配部署
  • STM32手册上标称的18MHz GPIO翻转速度和你实际测量到的速度之间的差异是预期之内且合理的
  • WebRTC中的几个Rtp*Sender
  • day028-Shell自动化编程-判断进阶
  • @Builder的用法
  • 【threejs】每天一个小案例讲解
  • 【数据结构】5. 双向链表
  • 《前端面试题:CSS3新特性》
  • Spring AI学习一
  • 【面经分享】京东
  • 【快速预览经典深度学习模型:CNN、RNN、LSTM、Transformer、ViT全解析!】
  • uv管理spaCy语言模型
  • vue2 跳转进入页面调用接口,刷新页面的时候不更新接口,利用页面存储进行判断写逻辑
  • 网站建设专家收费标准/百度指数专业版价格
  • 如何选择做网站/广告推广营销网站
  • 微信客服电话95068人工服务时间/百度关键词优化教程
  • 成都网站建设公司哪家好/网页设计与制作项目教程
  • 视屏网站制作/自助建站系统开发
  • 放网站的服务器吗/龙岗百度快速排名