在Axios请求拦截器JavaScript中停止请求

问题描述

我正在使用客户端节点应用程序中的Axios进行get呼叫。我正在尝试在应用程序中实现缓存机制。我有该应用程序的请求和响应拦截器。我试图基本上使用拦截器来检查数据是否在缓存中,如果是,我想取消请求并从缓存中返回响应。你能帮我怎么做。谢谢。以下是我的拦截器和提取调用。

import * as cache from '../cache-service'; //custom cache service  with get and put methods I wrote using IndexedDB

axios.interceptors.request.use((req)=>{
    if(!cache.get(req.param.id)){
       console.log(`${req.method} ${req.url}`);
       return req;
    }else{
       //TODO,how to cancel the request and return the response from cache?
    }
},(error)=>{
    console.log('In error section of request interceptor')
    return Promise.reject(error);
});

axios.interceptors.response.use((res)=>{
    console.log(res.data);
    return res;
},(error)=>{
    return Promise.reject(error);
});



export async function fetchData(ID) {
    let response = await axios.get("http://localhost:8443/api/"+"?id="+ID);
    return response;
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...