javascript - Axios - 拦截器:防止原始调用者进一步处理
问题描述
我的 Axios 实例有一个成功的响应拦截器。在某些情况下,我基本上想阻止.then()
's 和.catch()
's 的进一步执行。
axiosExtended.interceptors.response.use(response => successHandler(response), error => errorHandler(error))
function successHandler(response) {
const relativeRequestUrl = response.config.url;
const relativeResponseUrl = trim(response.request.responseURL.replace(response.config.baseURL, ''), '/');
if (relativeRequestUrl !== relativeResponseUrl) {
// dont return anything to original caller. No then. No catch.
router.push(relativeResponseUrl)
}
// get handled by original caller...
}
解决方案
感谢@felixmosh:
instance.interceptors.response.use((response) => {
if (someCondition(response) {
return new Promise(() => {});
}
return response;
});
推荐阅读
- javascript - FirebaseError:[code=invalid-argument]:函数 Query.where() 需要有效的第三个参数,但未定义
- python - 我的目录应用显示空白页,我该怎么办?
- html - 将 html 、 css 页面连接到页眉和页脚
- c# - ASP.NET Core 3.1 SignalR:未调用方法
- kotlin-coroutines - Kotlin Coroutines:Hot Flow 掌握线程
- react-native - 在带有 wix/react-native-navigation 的模态中使用 react-native-gesture-handler (RNGH)
- c - 是否可以查看/修改通用 Windows 虚拟打印驱动程序的代码以重定向“打印”文件的发送位置?
- python - 从漂亮的汤对象中提取文本时出现类型错误
- python - 在 Git Bash 中运行 pip freeze - TypeError: 'module' object is not callable
- mysql - 使用 information_schema.processlist 创建 MySQL 登录触发器?