码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • axios进阶——取消已经发出去的请求


    首先,引入取消已经发出去的请求的必要性:

    1. 防止重复请求:当用户快速连续触发同一操作(比如多次点击按钮发送同一个请求),这个函数可以确保只有最后一个请求被执行,之前的相同请求会被取消,从而避免了因重复请求导致的数据不一致或服务器压力。

    2. 优化性能和体验:通过维护一个待处理请求的映射表(pendingMap),可以有效管理网络请求队列,特别是在用户交互频繁的应用中,有助于提升应用响应速度和用户体验。

    3. 资源管理:及时取消不再需要的HTTP请求可以释放浏览器或应用的资源,尤其是在移动端或网络环境不稳定的情况下,有助于节省数据流量和电池使用。

    解决方案:CancelToken是axios库提供的一种机制,允许你取消已发出但尚未完成的HTTP请求。

    具体用法:

    1. axios.CancelToken((cancel) => {
    2. cancel()
    3. });

    封装进入axios中,大体思路为:

    1. 构建一个Map数据结构

    2.每发送一个网络请求,就将该请求映射入Map结构中

    3.存入前进行检查,如果存在该映射关系,将旧的请求cancel()取消掉,将新的映射存入

    一、封装取消axios的类

    1. let pendingMap = new Map<string, Canceler>();
    2. export const getPendingUrl = (config: AxiosRequestConfig) => [config.method, config.url].join('&');
    3. export class AxiosCanceler {
    4. /**
    5. * Add request
    6. * @param {Object} config
    7. */
    8. addPending(config: AxiosRequestConfig) {
    9. // 取消相同的请求
    10. this.removePending(config);
    11. // 构造key
    12. const url = getPendingUrl(config);
    13. config.cancelToken =
    14. config.cancelToken ||
    15. new axios.CancelToken((cancel) => {
    16. if (!pendingMap.has(url)) {
    17. // If there is no current request in pending, add it
    18. pendingMap.set(url, cancel);
    19. }
    20. });
    21. }
    22. /**
    23. * @description: Clear all pending
    24. */
    25. removeAllPending() {
    26. pendingMap.forEach((cancel) => {
    27. cancel && isFunction(cancel) && cancel();
    28. });
    29. pendingMap.clear();
    30. }
    31. /**
    32. * Removal request
    33. * @param {Object} config
    34. */
    35. removePending(config: AxiosRequestConfig) {
    36. const url = getPendingUrl(config);
    37. if (pendingMap.has(url)) {
    38. // If there is a current request identifier in pending,
    39. // the current request needs to be cancelled and removed
    40. const cancel = pendingMap.get(url);
    41. cancel && cancel(url);
    42. pendingMap.delete(url);
    43. }
    44. }
    45. /**
    46. * @description: reset
    47. */
    48. reset(): void {
    49. pendingMap = new Map<string, Canceler>();
    50. }
    51. }

    二、封装axios(部分)

    1. import { AxiosCanceler } from './axiosCancel';
    2. this.axiosInstance = axios.create(options);
    3. const axiosCanceler = new AxiosCanceler();
    4. this.axiosInstance.interceptors.request.use((config: InternalAxiosRequestConfig) => {
    5. axiosCanceler.addPending(config);
    6. return config;
    7. }, undefined);

  • 相关阅读:
    python自(2)切片 字典 遍历删除添加修改查询定义函数函数返回值作用域序列化异常报错urllib使用一个类型六个方法下载 视频音频图片
    Django框架之python后端框架介绍
    安全两方推理问题
    iptables防火墙
    大米自动化生产线的运作原理与科技创新
    【编程题】【Scratch二级】2021.03 寻找宝石
    Nacos整合OpenFegin实现RPC调用
    简约的博客网页制作 大学生个人博客网页设计模板 学生个人网页成品 DIV简单个人网站作品下载 静态HTML CSS个人网页作业源代码
    关于Highcharts图表的用法总结
    JavaWeb综合案例(黑马程序员2021年JavaWeb课程总结,所有功能均实现,包含数据库sql文件)
  • 原文地址:https://blog.csdn.net/liushihao_/article/details/139656383
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号