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

许昌市建设信息网站win7电脑做网站

许昌市建设信息网站,win7电脑做网站,做货代的要注册哪种物流网站,百度网首页官网一、请求失败会弹出 一个toast , 如何保证批量请求失败 , 只弹出一个toast 方法一:使用全局标志位 JavaScript let isToastShown false; // 全局标志位function makeRequests() {const requests [fetchPost(),fetchComments()]; // 多个请求Promise.all(reque…

一、请求失败会弹出 一个toast , 如何保证批量请求失败 , 只弹出一个toast 

方法一:使用全局标志位

JavaScript

let isToastShown = false; // 全局标志位function makeRequests() {const requests = [fetchPost(),fetchComments()]; // 多个请求Promise.all(requests).then(() => {// 所有请求成功的处理逻辑}).catch(errors => {if (!isToastShown) {// 检查标志位notify(errors[0]); // 弹出 toastisToastShown = true; // 设置标志位为 true}});
}function fetchJSON(url, input) {return fetch(url, input).then(res => {if (res.ok) {return res.json();}const err = new HttpError(res);if (!isToastShown) { // 检查标志位notify(err); // 弹出 toastisToastShown = true; // 设置标志位为 true}throw err; // 确保错误被抛出,否则后续逻辑可能无法捕获});
}// 示例错误类
class HttpError extends Error {constructor(response) {super(`${response.status} ${response.statusText}`);this.name = "HttpError";this.response = response;}
}// 重置标志位(在新的请求开始前)
makeRequests().finally(() => {isToastShown = false; // 确保在下一次请求时标志位被重置
});
方法二:使用防抖函数

JavaScript

// 防抖函数
function debounce(fn, delay = 1000) {let timeout;return function (...args) {clearTimeout(timeout);timeout = setTimeout(() => {fn.apply(this, args);}, delay);};
}const notifyDebounced = debounce(message => {notify(message);
}, 3000); // 3 秒内只执行一次// 示例调用
fetchJSON(url, input).catch(err => {notifyDebounced(err); // 使用防抖后的通知函数});
方法三:集中处理错误

JavaScript

function makeRequests() {const requests = [fetchPost(),fetchComments()]; // 多个请求Promise.allSettled(requests).then(results => {const hasError = results.some(result => result.status === "rejected");if (hasError) {const firstError = results.find(result => result.status === "rejected").reason;notify(firstError); // 合并所有错误并只弹出一次} else {// 所有请求成功的处理逻辑}});
}

二、减少项目中的 if-else 语句

1. 策略模式

JavaScript

// 定义策略对象
const strategies = {condition1: (arg) => {// 处理条件 1 的逻辑console.log(`Handling condition1 with argument: ${arg}`);},condition2: (arg) => {// 处理条件 2 的逻辑console.log(`Handling condition2 with argument: ${arg}`);},// 更多条件和处理逻辑
};// 动态获取条件并执行策略
function handleCondition(condition, arg) {const strategy = strategies[condition];if (strategy) {strategy(arg);} else {console.log(`Unknown condition: ${condition}`);}
}// 示例调用
handleCondition("condition1", "foo"); // 输出:Handling condition1 with argument: foo
handleCondition("condition3", "bar"); // 输出:Unknown condition: condition3
2. 表驱动法(动态条件)

JavaScript

const handlers = {"condition1": (arg) => console.log(`Handling condition1 with ${arg}`),"condition2": (arg) => console.log(`Handling condition2 with ${arg}`),// 其他条件处理函数
};function processConditions(conditions, arg) {conditions.forEach(condition => {const handler = handlers[condition];handler && handler(arg);});
}// 示例调用
processConditions(["condition1", "condition2"], "data"); // 输出两个条件的处理结果
3. 提取函数

JavaScript

function handleCaseA() {// 大量逻辑 A
}function handleCaseB() {// 大量逻辑 B
}function main() {const type = getType();if (type === "A") {handleCaseA();} else if (type === "B") {handleCaseB();} else {console.log("Unknown type");}
}
4. 状态模式

JavaScript

// 定义状态类
class State {handle(context) {console.log("Default state handling");}
}class StateA extends State {handle(context) {console.log("Handling State A");context.state = new StateB(); // 转换状态}
}class StateB extends State {handle(context) {console.log("Handling State B");context.state = new StateA();}
}// 上下文对象
class Context {constructor() {this.state = new StateA();}request() {this.state.handle(this);}
}// 示例调用
const context = new Context();
context.request(); // 输出:Handling State A
context.request(); // 输出:Handling State B

总结

  • 全局标志位:适用于简单的单次通知场景。

  • 防抖节流:适用于需要限制通知频率的场景。

  • 集中处理错误:适用于批量请求的错误统一管理。

  • 策略模式/表驱动法:将条件逻辑抽离为独立模块,增强可维护性。

  • 状态模式:适用于对象状态频繁变化的场景。

http://www.dtcms.com/a/578448.html

相关文章:

  • 7.无监督学习
  • 深圳软装公司排名前十强广州各区正在进一步优化以下措施
  • 电子商务网站开发公司做网站应注意什么
  • 网站建设技术服务公司陕西省国家示范校建设专题网站
  • Windows运行Kafka
  • 在阿里云备案网站通过上海虹口建设局官方网站
  • 廊坊专业网站制作服务前端开发是青春饭吗
  • 2025年11月6日 AI快讯
  • 我在高职教STM32(新10)——LCD1602驱动程序分析
  • 深圳市公司网站建设平台精品网站建设多少钱
  • 资源站建站技术贵州最新新闻
  • 代理模式的使用
  • 什么网站好建设做一个公司官网需要多少钱
  • 微博如何做的跟网站一样asp网站做视频
  • 万网网站备案查询什么网站做美食最好最专业
  • 做网站首页的图片怎么缩小网络优化工程师前景
  • 做loge的网站山东省建设厅官方网站怎么样
  • 1.1 计算机系统
  • Windows 使用Mongosh创建管理与普通账户
  • 【前端】JS/JQ实现页面滚动控制
  • Javascript数组案例-生成柱形图
  • 盛雪竹的命运轨迹与菅纫姿的表演解析
  • javaFX AutoCompleteComboBox / AutoCompletionBinding 控件jar包下载
  • 天元建设集团有限公司分公司百度seo排名公司
  • 校园网站建设年度工作计划垂直门户网站怎么做
  • 贵阳网站开发哪家便宜中国家装公司十大排名
  • rk3568-android11-fan-pwm-temp
  • 比较好的设计网站推荐成都网站建设 工资
  • 建行网站济南电子简历手机版免费
  • 建设银行大冶支行网站wordpress如何改字体