首页 文章 精选 留言 我的

精选列表

搜索[javascript正则],共10000篇文章
优秀的个人博客,低调大师

C# 判断字符串是否符合十六进制,八进制,二进制和十进制整数格式的正则表达式

/// <summary> /// 判断是否十六进制格式字符串 /// </summary> /// <param name="str"></param> /// <returns></returns> public bool IsHexadecimal(string str) { const string PATTERN = @"[A-Fa-f0-9]+$"; return System.Text.RegularExpressions.Regex.IsMatch(str, PATTERN); } /// <summary> /// 判断是否八进制格式字符串 /// </summary> /// <param name="str"></param> /// <returns></returns> public bool IsOctal(string str) { const string PATTERN = @"[0-7]+$"; return System.Text.RegularExpressions.Regex.IsMatch(str, PATTERN); } /// <summary> /// 判断是否二进制格式字符串 /// </summary> /// <param name="str"></param> /// <returns></returns> public bool IsBinary(string str) { const string PATTERN = @"[0-1]+$"; return System.Text.RegularExpressions.Regex.IsMatch(str, PATTERN); } /// <summary> /// 判断是否十进制格式字符串 /// </summary> /// <param name="str"></param> /// <returns></returns> public bool IsDecimal(string str) { const string PATTERN = @"[0-9]+$"; return System.Text.RegularExpressions.Regex.IsMatch(str, PATTERN); }

优秀的个人博客,低调大师

JavaScript 事件循环中的微任务执行时机:一次真实的 Bug 排查记录

上个月在维护一个用户行为上报 SDK 时遇到了一个诡异的问题:页面上某个按钮的点击回调里,同步修改了状态变量,紧接着用 Promise.then 发送上报请求,再在 setTimeout 里读取状态变量,结果读到的竟然是修改前的旧值。代码逻辑看起来没问题,状态确实在同步代码里改了,但异步回调里拿到的却是旧值。排查后发现是事件循环中微任务与宏任务的执行顺序被我们误解了。微任务在每次宏任务执行完、当前调用栈清空后立即执行,而不是等到下一个宏任务。如果微任务里又产生了新的微任务,会一直清空微任务队列再进入下一个宏任务。这个机制在 Promise、queueMicrotask、MutationObserver 里都会体现,也直接影响 Vue 的 nextTick 和 React 的批处理行为。文章结合这次排查过程,把微任务的执行时机、常见误区和实际规避方式拆开讲清楚,代码都来自真实项目。

优秀的个人博客,低调大师

JavaScript 中 structuredClone 的实战坑:为什么你的深拷贝在 class 实例上翻车了?

在维护一个后台管理系统时,我需要把一份包含日期、Map 和自定义 class 实例的配置对象深拷贝到草稿状态里。以前一直用 JSON.parse(JSON.stringify(obj)),但它会把 Date 变成字符串、丢掉 undefined,更麻烦的是遇到 class 实例直接变成普通对象,方法全没了。后来我换成了浏览器原生的 structuredClone,大部分场景确实好使,但有一次拷贝一个带 getter 的 class 实例时,拷贝出来的对象属性直接变成 undefined,排查了半天才发现 structuredClone 虽然能处理循环引用和内置类型,但它不会保留原型链。这篇文章从实际代码出发,讲清楚 structuredClone 和 JSON 深拷贝的边界在哪、哪些数据结构它会静默丢信息、以及怎么用 MessageChannel 或手写递归补上原型和 getter 的坑,顺便对比 lodash.cloneDeep 在 class 场景下的表现。 ```js class User { constructor(name) { this.name = name; this.createdAt = new Date(); } get displayName() { return `用户:${this.name}`; } greet() { return `hello ${this.name}`; } } const u = new User('alice'); const cloned = structuredClone(u); console.log(cloned instanceof User); // false console.log(cloned.displayName); // undefined console.log(typeof cloned.greet); // 'undefined' console.log(cloned.name); // 'alice' ``` 上面这段代码说明问题:structuredClone 会老老实实复制自有属性,但原型上的 getter 和方法完全不会被带过去。它内部用的是结构化克隆算法,只搬运“数据”,不搬运行为。如果你把 class 实例当作纯数据容器,那没问题;一旦代码依赖原型方法,拷贝结果就是残缺的。 再看它和 JSON 方案的差异。structuredClone 能正确处理 Date、RegExp、Map、Set、ArrayBuffer、循环引用,这些是 JSON 方案做不到的。 ```js const source = { date: new Date(), map: new Map([['a', 1]]), set: new Set([1, 2]), regex: /abc/g, buffer: new ArrayBuffer(8), }; source.self = source; // 循环引用 const cloned = structuredClone(source); console.log(cloned.date instanceof Date); // true console.log(cloned.map.get('a')); // 1 console.log(cloned.self === cloned); // true ``` JSON 方案遇到循环引用直接抛 TypeError,遇到 Map 会变成空对象 `{}`,遇到 Date 变成 ISO 字符串。所以只要数据结构里出现这些类型,structuredClone 是首选。但它也有明确不支持的:函数、DOM 节点、Error 的某些子类、以及带原型的对象。函数会直接抛 DataCloneError。 ```js structuredClone({ fn: () => {} }); // Uncaught DOMException: Failed to execute 'structuredClone' on 'Window': () => {} could not be cloned. ``` 实际项目里更隐蔽的坑是 getter。structuredClone 在复制时会对源对象做一次“读属性”操作,如果 getter 内部有副作用或者依赖 this 上的其他状态,读出来的值可能和你预期不同。而且拷贝结果里 getter 会退化成普通数据属性,后续不再动态计算。 ```js class Config { constructor() { this._base = 10; } get total() { return this._base * 2; } } const c = new Config(); const copy = structuredClone(c); console.log(copy.total); // 20,但已经是普通属性 copy._base = 100; console.log(copy.total); // 仍然是 20,不再联动 ``` 如果业务确实需要保留原型和 getter,可以借助 MessageChannel 做一次结构化克隆,再手动把原型接回去。但更直接的办法是给 class 写一个 `clone()` 方法,显式重建实例。 ```js class User { constructor(name, createdAt = new Date()) { this.name = name; this.createdAt = createdAt; } clone() { return new User(this.name, new Date(this.createdAt)); } } const u = new User('bob'); const copy = u.clone(); console.log(copy instanceof User); // true console.log(copy.greet()); // hello bob ``` 对于嵌套层级深、class 类型多的配置对象,lodash 的 `cloneDeep` 会保留原型,但它对 Map、Set 的支持要看版本,早期版本直接当普通对象处理。在 lodash 4.17 之后 `cloneDeep` 能正确复制 Map 和 Set,但循环引用和 Symbol 键仍然有边界情况。所以选型时先看清数据结构:纯 JSON 数据用 JSON 方案最快;含 Date/Map/Set/循环引用用 structuredClone;需要保留原型和方法就手写 clone 或组合使用。 还有一个容易忽略的点:structuredClone 是同步的,如果拷贝的对象特别大,比如几十兆的 ArrayBuffer,主线程会卡住。这种场景可以放到 Web Worker 里做,或者用 `postMessage` 天然走结构化克隆,顺便把计算移出主线程。 ```js // worker.js self.onmessage = (e) => { const cloned = structuredClone(e.data); self.postMessage(cloned); }; ``` 浏览器兼容性方面,Chrome 98+、Firefox 94+、Safari 15.4+ 都支持 structuredClone。Node.js 从 17 开始提供全局 structuredClone,之前版本需要 `require('node:v8').serialize` 做替代,但 v8 序列化不支持循环引用以外的某些类型。如果项目还要兼容旧环境,建议封装一层降级逻辑。 ```js function deepClone(value) { if (typeof structuredClone === 'function') { return structuredClone(value); } return JSON.parse(JSON.stringify(value)); } ``` 这个降级只适合纯数据场景,别指望它处理 Date 和 Map。真正稳妥的做法还是根据数据类型走不同分支,而不是无脑深拷贝。很多情况下,避免深拷贝、改用不可变更新反而更省心。 推荐查看天气:https://www.40tianqi.com/xichuan/ 推荐查看天气:https://www.40tianqi.com/xichuan/zuotian.html 推荐查看天气:https://www.40tianqi.com/xichuan/7tian.html 推荐查看天气:https://www.40tianqi.com/xichuan/10tian.html 推荐查看天气:https://www.40tianqi.com/xichuan/15tian.html 推荐查看天气:https://www.40tianqi.com/xichuan/40tian.html 推荐查看天气:https://www.40tianqi.com/sheqi/ 推荐查看天气:https://www.40tianqi.com/sheqi/zuotian.html 推荐查看天气:https://www.40tianqi.com/sheqi/7tian.html 推荐查看天气:https://www.40tianqi.com/sheqi/10tian.html 推荐查看天气:https://www.40tianqi.com/sheqi/15tian.html 推荐查看天气:https://www.40tianqi.com/sheqi/40tian.html 推荐查看天气:https://www.40tianqi.com/tanghe/ 推荐查看天气:https://www.40tianqi.com/tanghe/zuotian.html 推荐查看天气:https://www.40tianqi.com/tanghe/7tian.html 推荐查看天气:https://www.40tianqi.com/tanghe/10tian.html 推荐查看天气:https://www.40tianqi.com/tanghe/15tian.html 推荐查看天气:https://www.40tianqi.com/tanghe/40tian.html 推荐查看天气:https://www.40tianqi.com/xinye/ 推荐查看天气:https://www.40tianqi.com/xinye/zuotian.html 推荐查看天气:https://www.40tianqi.com/xinye/7tian.html 推荐查看天气:https://www.40tianqi.com/xinye/10tian.html 推荐查看天气:https://www.40tianqi.com/xinye/15tian.html 推荐查看天气:https://www.40tianqi.com/xinye/40tian.html 推荐查看天气:https://www.40tianqi.com/tongbai/ 推荐查看天气:https://www.40tianqi.com/tongbai/zuotian.html 推荐查看天气:https://www.40tianqi.com/tongbai/7tian.html 推荐查看天气:https://www.40tianqi.com/tongbai/10tian.html 推荐查看天气:https://www.40tianqi.com/tongbai/15tian.html 推荐查看天气:https://www.40tianqi.com/tongbai/40tian.html 推荐查看天气:https://www.40tianqi.com/dengzhou/ 推荐查看天气:https://www.40tianqi.com/dengzhou/zuotian.html 推荐查看天气:https://www.40tianqi.com/dengzhou/7tian.html 推荐查看天气:https://www.40tianqi.com/dengzhou/10tian.html 推荐查看天气:https://www.40tianqi.com/dengzhou/15tian.html 推荐查看天气:https://www.40tianqi.com/dengzhou/40tian.html 推荐查看天气:https://www.40tianqi.com/shangqiu/ 推荐查看天气:https://www.40tianqi.com/shangqiu/zuotian.html 推荐查看天气:https://www.40tianqi.com/shangqiu/7tian.html 推荐查看天气:https://www.40tianqi.com/shangqiu/10tian.html 推荐查看天气:https://www.40tianqi.com/shangqiu/15tian.html 推荐查看天气:https://www.40tianqi.com/shangqiu/40tian.html 推荐查看天气:https://www.40tianqi.com/liangyuan/ 推荐查看天气:https://www.40tianqi.com/liangyuan/zuotian.html 推荐查看天气:https://www.40tianqi.com/liangyuan/7tian.html 推荐查看天气:https://www.40tianqi.com/liangyuan/10tian.html 推荐查看天气:https://www.40tianqi.com/liangyuan/15tian.html 推荐查看天气:https://www.40tianqi.com/liangyuan/40tian.html 推荐查看天气:https://www.40tianqi.com/minquan/ 推荐查看天气:https://www.40tianqi.com/minquan/zuotian.html 推荐查看天气:https://www.40tianqi.com/minquan/7tian.html 推荐查看天气:https://www.40tianqi.com/minquan/10tian.html 推荐查看天气:https://www.40tianqi.com/minquan/15tian.html 推荐查看天气:https://www.40tianqi.com/minquan/40tian.html 推荐查看天气:https://www.40tianqi.com/suixian/ 推荐查看天气:https://www.40tianqi.com/suixian/zuotian.html 推荐查看天气:https://www.40tianqi.com/suixian/7tian.html 推荐查看天气:https://www.40tianqi.com/suixian/10tian.html 推荐查看天气:https://www.40tianqi.com/suixian/15tian.html 推荐查看天气:https://www.40tianqi.com/suixian/40tian.html 推荐查看天气:https://www.40tianqi.com/ningling/ 推荐查看天气:https://www.40tianqi.com/ningling/zuotian.html 推荐查看天气:https://www.40tianqi.com/ningling/7tian.html 推荐查看天气:https://www.40tianqi.com/ningling/10tian.html 推荐查看天气:https://www.40tianqi.com/ningling/15tian.html 推荐查看天气:https://www.40tianqi.com/ningling/40tian.html 推荐查看天气:https://www.40tianqi.com/zhecheng/ 推荐查看天气:https://www.40tianqi.com/zhecheng/zuotian.html 推荐查看天气:https://www.40tianqi.com/zhecheng/7tian.html 推荐查看天气:https://www.40tianqi.com/zhecheng/10tian.html 推荐查看天气:https://www.40tianqi.com/zhecheng/15tian.html 推荐查看天气:https://www.40tianqi.com/zhecheng/40tian.html 推荐查看天气:https://www.40tianqi.com/shangqiuyucheng/ 推荐查看天气:https://www.40tianqi.com/shangqiuyucheng/zuotian.html 推荐查看天气:https://www.40tianqi.com/shangqiuyucheng/7tian.html 推荐查看天气:https://www.40tianqi.com/shangqiuyucheng/10tian.html 推荐查看天气:https://www.40tianqi.com/shangqiuyucheng/15tian.html 推荐查看天气:https://www.40tianqi.com/shangqiuyucheng/40tian.html 推荐查看天气:https://www.40tianqi.com/xiayi/ 推荐查看天气:https://www.40tianqi.com/xiayi/zuotian.html 推荐查看天气:https://www.40tianqi.com/xiayi/7tian.html 推荐查看天气:https://www.40tianqi.com/xiayi/10tian.html 推荐查看天气:https://www.40tianqi.com/xiayi/15tian.html 推荐查看天气:https://www.40tianqi.com/xiayi/40tian.html 推荐查看天气:https://www.40tianqi.com/xinyang/ 推荐查看天气:https://www.40tianqi.com/xinyang/zuotian.html 推荐查看天气:https://www.40tianqi.com/xinyang/7tian.html 推荐查看天气:https://www.40tianqi.com/xinyang/10tian.html 推荐查看天气:https://www.40tianqi.com/xinyang/15tian.html 推荐查看天气:https://www.40tianqi.com/xinyang/40tian.html 推荐查看天气:https://www.40tianqi.com/shihe/ 推荐查看天气:https://www.40tianqi.com/shihe/zuotian.html 推荐查看天气:https://www.40tianqi.com/shihe/7tian.html 推荐查看天气:https://www.40tianqi.com/shihe/10tian.html 推荐查看天气:https://www.40tianqi.com/shihe/15tian.html 推荐查看天气:https://www.40tianqi.com/shihe/40tian.html 推荐查看天气:https://www.40tianqi.com/pingqiao/ 推荐查看天气:https://www.40tianqi.com/pingqiao/zuotian.html 推荐查看天气:https://www.40tianqi.com/pingqiao/7tian.html 推荐查看天气:https://www.40tianqi.com/pingqiao/10tian.html 推荐查看天气:https://www.40tianqi.com/pingqiao/15tian.html 推荐查看天气:https://www.40tianqi.com/pingqiao/40tian.html 推荐查看天气:https://www.40tianqi.com/luoshan/ 推荐查看天气:https://www.40tianqi.com/luoshan/zuotian.html 推荐查看天气:https://www.40tianqi.com/luoshan/7tian.html 推荐查看天气:https://www.40tianqi.com/luoshan/10tian.html 推荐查看天气:https://www.40tianqi.com/luoshan/15tian.html 推荐查看天气:https://www.40tianqi.com/luoshan/40tian.html 推荐查看天气:https://www.40tianqi.com/guangshan/ 推荐查看天气:https://www.40tianqi.com/guangshan/zuotian.html 推荐查看天气:https://www.40tianqi.com/guangshan/7tian.html 推荐查看天气:https://www.40tianqi.com/guangshan/10tian.html 推荐查看天气:https://www.40tianqi.com/guangshan/15tian.html 推荐查看天气:https://www.40tianqi.com/guangshan/40tian.html 推荐查看天气:https://www.40tianqi.com/xinyangxinxian/ 推荐查看天气:https://www.40tianqi.com/xinyangxinxian/zuotian.html 推荐查看天气:https://www.40tianqi.com/xinyangxinxian/7tian.html 推荐查看天气:https://www.40tianqi.com/xinyangxinxian/10tian.html 推荐查看天气:https://www.40tianqi.com/xinyangxinxian/15tian.html 推荐查看天气:https://www.40tianqi.com/xinyangxinxian/40tian.html 推荐查看天气:https://www.40tianqi.com/shangcheng/ 推荐查看天气:https://www.40tianqi.com/shangcheng/zuotian.html 推荐查看天气:https://www.40tianqi.com/shangcheng/7tian.html 推荐查看天气:https://www.40tianqi.com/shangcheng/10tian.html 推荐查看天气:https://www.40tianqi.com/shangcheng/15tian.html 推荐查看天气:https://www.40tianqi.com/shangcheng/40tian.html 推荐查看天气:https://www.40tianqi.com/gushi/ 推荐查看天气:https://www.40tianqi.com/gushi/zuotian.html 推荐查看天气:https://www.40tianqi.com/gushi/7tian.html 推荐查看天气:https://www.40tianqi.com/gushi/10tian.html 推荐查看天气:https://www.40tianqi.com/gushi/15tian.html 推荐查看天气:https://www.40tianqi.com/gushi/40tian.html 推荐查看天气:https://www.40tianqi.com/huangchuan/ 推荐查看天气:https://www.40tianqi.com/huangchuan/zuotian.html 推荐查看天气:https://www.40tianqi.com/huangchuan/7tian.html 推荐查看天气:https://www.40tianqi.com/huangchuan/10tian.html 推荐查看天气:https://www.40tianqi.com/huangchuan/15tian.html 推荐查看天气:https://www.40tianqi.com/huangchuan/40tian.html 推荐查看天气:https://www.40tianqi.com/huaibin/ 推荐查看天气:https://www.40tianqi.com/huaibin/zuotian.html 推荐查看天气:https://www.40tianqi.com/huaibin/7tian.html 推荐查看天气:https://www.40tianqi.com/huaibin/10tian.html 推荐查看天气:https://www.40tianqi.com/huaibin/15tian.html 推荐查看天气:https://www.40tianqi.com/huaibin/40tian.html 推荐查看天气:https://www.40tianqi.com/xixian/ 推荐查看天气:https://www.40tianqi.com/xixian/zuotian.html 推荐查看天气:https://www.40tianqi.com/xixian/7tian.html 推荐查看天气:https://www.40tianqi.com/xixian/10tian.html 推荐查看天气:https://www.40tianqi.com/xixian/15tian.html 推荐查看天气:https://www.40tianqi.com/xixian/40tian.html 推荐查看天气:https://www.40tianqi.com/zhoukou/ 推荐查看天气:https://www.40tianqi.com/zhoukou/zuotian.html 推荐查看天气:https://www.40tianqi.com/zhoukou/7tian.html 推荐查看天气:https://www.40tianqi.com/zhoukou/10tian.html 推荐查看天气:https://www.40tianqi.com/zhoukou/15tian.html 推荐查看天气:https://www.40tianqi.com/zhoukou/40tian.html 推荐查看天气:https://www.40tianqi.com/chuanhui/ 推荐查看天气:https://www.40tianqi.com/chuanhui/zuotian.html 推荐查看天气:https://www.40tianqi.com/chuanhui/7tian.html 推荐查看天气:https://www.40tianqi.com/chuanhui/10tian.html 推荐查看天气:https://www.40tianqi.com/chuanhui/15tian.html 推荐查看天气:https://www.40tianqi.com/chuanhui/40tian.html 推荐查看天气:https://www.40tianqi.com/fugou/ 推荐查看天气:https://www.40tianqi.com/fugou/zuotian.html 推荐查看天气:https://www.40tianqi.com/fugou/7tian.html 推荐查看天气:https://www.40tianqi.com/fugou/10tian.html 推荐查看天气:https://www.40tianqi.com/fugou/15tian.html 推荐查看天气:https://www.40tianqi.com/fugou/40tian.html 推荐查看天气:https://www.40tianqi.com/xihua/ 推荐查看天气:https://www.40tianqi.com/xihua/zuotian.html 推荐查看天气:https://www.40tianqi.com/xihua/7tian.html 推荐查看天气:https://www.40tianqi.com/xihua/10tian.html 推荐查看天气:https://www.40tianqi.com/xihua/15tian.html 推荐查看天气:https://www.40tianqi.com/xihua/40tian.html 推荐查看天气:https://www.40tianqi.com/shangshui/ 推荐查看天气:https://www.40tianqi.com/shangshui/zuotian.html 推荐查看天气:https://www.40tianqi.com/shangshui/7tian.html 推荐查看天气:https://www.40tianqi.com/shangshui/10tian.html 推荐查看天气:https://www.40tianqi.com/shangshui/15tian.html 推荐查看天气:https://www.40tianqi.com/shangshui/40tian.html 推荐查看天气:https://www.40tianqi.com/shenqiu/

优秀的个人博客,低调大师

JavaScript 里 Promise 链中 then 回调抛错后,后面的 catch 为什么有时接不住?

写业务代码时经常遇到这种状况:一个接口请求失败后,你在 `.then()` 里做了错误处理,但代码还是走到了全局的 `unhandledrejection` 事件,控制台飘红。或者反过来,明明某个 `.then()` 里 `throw new Error()` 了,后面的 `.catch()` 却像没看见一样,最后错误跑到了另一个分支里。这不是 Promise 实现有 bug,而是很多人对 then 链的返回值传递和错误冒泡机制理解得不够完整。Promise 的错误处理依赖链式调用中每个环节的返回状态,一旦在某个 `then` 里把 rejection 消化掉,或者返回了一个新的 Promise 把错误吞了,后续的 catch 就永远接不到。这篇文章会从几个真实的代码片段出发,梳理 then 链里错误是怎么一级一级往下传的,以及为什么有时 catch 写了却没生效。 ## 一个容易踩的坑:在 then 里 return 了新的 Promise 先看一段常见的登录逻辑: ```js function login(username, password) { return fetch('/api/login', { method: 'POST', body: JSON.stringify({ username, password }) }) .then(res => res.json()) .then(data => { if (data.code !== 0) { // 这里只是返回了一个 rejected 的 Promise return Promise.reject(new Error(data.message)) } return data.token }) .catch(err => { console.error('login failed:', err) throw err }) } login('foo', 'bar') .then(token => { // 期望在这里拿到 token }) .catch(err => { // 期望在这里处理登录错误 }) ``` 这段代码本身没有大问题,`login` 内部的 `.catch` 把错误重新抛出去,所以外层的 `.catch` 能接到。但很多人在写的时候会在 `catch` 里返回 `false` 或者一个默认值,比如: ```js .catch(err => { console.error(err) return null }) ``` 这个时候链式调用的状态从 rejected 变成了 fulfilled,外层的 `.then(token => ...)` 会被执行,token 是 `null`,而外层后面的 `.catch` 根本不会触发。如果你没在 `then` 里检查 token 是否为空,后续逻辑就可能把 null 传下去,引发更难排查的问题。 ## 错误在 then 链里的传递规则 理解错误传递,只要记住两件事: 1. `.then(onFulfilled, onRejected)` 里如果 `onFulfilled` 抛出异常,返回的 Promise 会变成 rejected;如果 `onRejected` 抛出异常,同样变成 rejected;如果 `onRejected` 返回一个普通值,Promise 会变成 fulfilled。 2. `.catch(onRejected)` 本质上是 `.then(undefined, onRejected)`,所以它也只能捕获它前面那个 Promise 的 rejection。 看一个对比: ```js Promise.reject(new Error('a')) .then(() => console.log('then')) .catch(err => { console.log('catch1', err.message) // catch1 a // 没有 throw,返回 undefined }) .catch(err => { console.log('catch2', err.message) // 不会执行 }) ``` 第一个 `catch` 返回了 `undefined`,相当于把错误处理掉了,后面就没有 rejection 再往下传。如果希望后面的 catch 还能拿到,就必须在 catch 里重新 throw: ```js Promise.reject(new Error('a')) .catch(err => { console.log('catch1', err.message) throw err }) .catch(err => { console.log('catch2', err.message) // catch2 a }) ``` 这个规则本身不复杂,但在嵌套 Promise 或者多个 then 串联时容易混乱,因为每个 then 都会产生一个新的 Promise。 ## 嵌套 Promise 里的错误不会自动冒泡到外层链 有时候我们会这样写: ```js fetch('/api/user') .then(res => res.json()) .then(user => { // 又发起一个请求,但没有 return fetch(`/api/orders/${user.id}`) .then(res => res.json()) .then(orders => { renderOrders(orders) }) }) .catch(err => { console.error('something wrong', err) }) ``` 如果 `/api/orders` 那个请求失败了,内层的 Promise 会 reject,但因为没有 `return`,外层链根本不知道这个新 Promise 的存在。内层的 rejection 会变成一个未处理的 Promise rejection,触发 `unhandledrejection`,而外层的 `.catch` 完全接不到。 正确的做法是把这个内层 Promise 返回出去,让链式调用保持扁平: ```js fetch('/api/user') .then(res => res.json()) .then(user => { return fetch(`/api/orders/${user.id}`) .then(res => res.json()) }) .then(orders => { renderOrders(orders) }) .catch(err => { console.error('something wrong', err) }) ``` 这样内层的错误才会沿着 Promise 链传到外层 catch。 ## async/await 里同样的逻辑 用 `async/await` 时,错误传递规则没变,只是写法不同。`await` 一个 rejected Promise 会抛出异常,可以用 `try/catch` 捕获: ```js async function loadUser() { try { const res = await fetch('/api/user') const user = await res.json() return user } catch (err) { console.error('load user failed', err) throw err } } ``` 如果在 `catch` 里没有 `throw err`,那么 `loadUser` 返回的 Promise 会变成 fulfilled,调用方用 `.catch` 是接不到的。这和前面 `.then` 链的行为一致。 还有一个容易忽略的点:在 `async` 函数里,如果你在 `await` 之后同步抛出一个错误,和 `await` 一个 rejected Promise 一样,都会让函数返回 rejected Promise。 ```js async function getData() { const res = await fetch('/api/data') if (!res.ok) { throw new Error('request failed') // 函数返回 rejected Promise } return res.json() } getData() .then(data => console.log(data)) .catch(err => console.error(err)) // 能接住 ``` 但如果调用方没有写 catch,并且也没有用 `try/catch` 包住 await,错误就会变成 `unhandledrejection`。 ## 什么时候该重新 throw,什么时候该消化错误 这其实是一个业务决策,不是语法问题。判断标准很简单:当前这个 catch 能不能把错误处理完整? 比如在某个子流程里,请求配置失败,但可以降级到本地缓存,这种错误就应该被消化掉,不要往外抛: ```js async function getConfig() { try { const res = await fetch('/api/config') return await res.json() } catch (err) { console.warn('remote config failed, fallback to local') return localConfig } } ``` 如果当前层只是记录日志,并不具备恢复能力,就应该把错误继续抛出去,让上层决定怎么处理: ```js async function updateProfile(data) { try { await api.put('/profile', data) } catch (err) { logError(err) throw err // 让调用方知道更新失败 } } ``` 最怕的是在中间层既没有恢复,又默默返回了 `undefined`,导致上层拿到一个“成功但没数据”的结果,后续逻辑全乱。 ## 调试 unhandledrejection 的一个实用手段 Chrome DevTools 的 Console 里可以打开 “Pause on caught exceptions” 和 “Pause on uncaught exceptions”,配合 `window.addEventListener('unhandledrejection', event => { ... })` 来定位是哪个 Promise 没有被处理。 ```js window.addEventListener('unhandledrejection', event => { // event.reason 是 rejection 的原因 console.error('Unhandled rejection:', event.reason) // 可以在这里上报监控 }) ``` 在 Node.js 里类似: ```js process.on('unhandledRejection', (reason, promise) => { console.error('Unhandled rejection at:', promise, 'reason:', reason) }) ``` 但要注意,`unhandledrejection` 只在 rejection 发生后没有任何处理程序时才会触发。如果在一个 Promise 之后很晚才加上 catch,浏览器可能已经触发过这个事件了。所以生产环境里更推荐在 Promise 链的末端统一处理,而不是依赖全局事件兜底。 ## 一个实际场景:多个并发请求里某个失败 用 `Promise.all` 时,只要有一个 Promise reject,整体就会 reject,其他 Promise 的结果会被忽略。如果想让每个请求独立处理错误,可以用 `Promise.allSettled`: ```js const results = await Promise.allSettled([ fetch('/api/a').then(res => res.json()), fetch('/api/b').then(res => res.json()), fetch('/api/c').then(res => res.json()) ]) results.forEach((result, index) => { if (result.status === 'fulfilled') { console.log(`request ${index} success:`, result.value) } else { console.error(`request ${index} failed:`, result.reason) } }) ``` 如果坚持用 `Promise.all`,就需要在单个请求的 catch 里决定是重新抛出让整体失败,还是返回一个占位值让整体继续。两种选择没有对错,取决于业务是否允许部分数据缺失。 Promise 的错误处理看起来只是 `.catch` 的位置问题,实际写起来会因为返回值、嵌套、async 函数的隐式转换产生很多分支。遇到 catch 接不住的情况,先检查前一个环节是不是把错误消化了,再检查有没有漏掉 return。多数时候问题就在这两处。 推荐查看天气:https://www.0dtq.com/dongtou/ 推荐查看天气:https://www.0dtq.com/dongtou/zuotian.html 推荐查看天气:https://www.0dtq.com/dongtou/7tian.html 推荐查看天气:https://www.0dtq.com/dongtou/10tian.html 推荐查看天气:https://www.0dtq.com/dongtou/15tian.html 推荐查看天气:https://www.0dtq.com/dongtou/40tian.html 推荐查看天气:https://www.0dtq.com/yongjia/ 推荐查看天气:https://www.0dtq.com/yongjia/zuotian.html 推荐查看天气:https://www.0dtq.com/yongjia/7tian.html 推荐查看天气:https://www.0dtq.com/yongjia/10tian.html 推荐查看天气:https://www.0dtq.com/yongjia/15tian.html 推荐查看天气:https://www.0dtq.com/yongjia/40tian.html 推荐查看天气:https://www.0dtq.com/pingyang/ 推荐查看天气:https://www.0dtq.com/pingyang/zuotian.html 推荐查看天气:https://www.0dtq.com/pingyang/7tian.html 推荐查看天气:https://www.0dtq.com/pingyang/10tian.html 推荐查看天气:https://www.0dtq.com/pingyang/15tian.html 推荐查看天气:https://www.0dtq.com/pingyang/40tian.html 推荐查看天气:https://www.0dtq.com/cangnan/ 推荐查看天气:https://www.0dtq.com/cangnan/zuotian.html 推荐查看天气:https://www.0dtq.com/cangnan/7tian.html 推荐查看天气:https://www.0dtq.com/cangnan/10tian.html 推荐查看天气:https://www.0dtq.com/cangnan/15tian.html 推荐查看天气:https://www.0dtq.com/cangnan/40tian.html 推荐查看天气:https://www.0dtq.com/wencheng/ 推荐查看天气:https://www.0dtq.com/wencheng/zuotian.html 推荐查看天气:https://www.0dtq.com/wencheng/7tian.html 推荐查看天气:https://www.0dtq.com/wencheng/10tian.html 推荐查看天气:https://www.0dtq.com/wencheng/15tian.html 推荐查看天气:https://www.0dtq.com/wencheng/40tian.html 推荐查看天气:https://www.0dtq.com/taishun/ 推荐查看天气:https://www.0dtq.com/taishun/zuotian.html 推荐查看天气:https://www.0dtq.com/taishun/7tian.html 推荐查看天气:https://www.0dtq.com/taishun/10tian.html 推荐查看天气:https://www.0dtq.com/taishun/15tian.html 推荐查看天气:https://www.0dtq.com/taishun/40tian.html 推荐查看天气:https://www.0dtq.com/ruian/ 推荐查看天气:https://www.0dtq.com/ruian/zuotian.html 推荐查看天气:https://www.0dtq.com/ruian/7tian.html 推荐查看天气:https://www.0dtq.com/ruian/10tian.html 推荐查看天气:https://www.0dtq.com/ruian/15tian.html 推荐查看天气:https://www.0dtq.com/ruian/40tian.html 推荐查看天气:https://www.0dtq.com/leqing/ 推荐查看天气:https://www.0dtq.com/leqing/zuotian.html 推荐查看天气:https://www.0dtq.com/leqing/7tian.html 推荐查看天气:https://www.0dtq.com/leqing/10tian.html 推荐查看天气:https://www.0dtq.com/leqing/15tian.html 推荐查看天气:https://www.0dtq.com/leqing/40tian.html 推荐查看天气:https://www.0dtq.com/jiaxing/ 推荐查看天气:https://www.0dtq.com/jiaxing/zuotian.html 推荐查看天气:https://www.0dtq.com/jiaxing/7tian.html 推荐查看天气:https://www.0dtq.com/jiaxing/10tian.html 推荐查看天气:https://www.0dtq.com/jiaxing/15tian.html 推荐查看天气:https://www.0dtq.com/jiaxing/40tian.html 推荐查看天气:https://www.0dtq.com/xiuzhou/ 推荐查看天气:https://www.0dtq.com/xiuzhou/zuotian.html 推荐查看天气:https://www.0dtq.com/xiuzhou/7tian.html 推荐查看天气:https://www.0dtq.com/xiuzhou/10tian.html 推荐查看天气:https://www.0dtq.com/xiuzhou/15tian.html 推荐查看天气:https://www.0dtq.com/xiuzhou/40tian.html 推荐查看天气:https://www.0dtq.com/jiashan/ 推荐查看天气:https://www.0dtq.com/jiashan/zuotian.html 推荐查看天气:https://www.0dtq.com/jiashan/7tian.html 推荐查看天气:https://www.0dtq.com/jiashan/10tian.html 推荐查看天气:https://www.0dtq.com/jiashan/15tian.html 推荐查看天气:https://www.0dtq.com/jiashan/40tian.html 推荐查看天气:https://www.0dtq.com/jiaxinghaiyan/ 推荐查看天气:https://www.0dtq.com/jiaxinghaiyan/zuotian.html 推荐查看天气:https://www.0dtq.com/jiaxinghaiyan/7tian.html 推荐查看天气:https://www.0dtq.com/jiaxinghaiyan/10tian.html 推荐查看天气:https://www.0dtq.com/jiaxinghaiyan/15tian.html 推荐查看天气:https://www.0dtq.com/jiaxinghaiyan/40tian.html 推荐查看天气:https://www.0dtq.com/haining/ 推荐查看天气:https://www.0dtq.com/haining/zuotian.html 推荐查看天气:https://www.0dtq.com/haining/7tian.html 推荐查看天气:https://www.0dtq.com/haining/10tian.html 推荐查看天气:https://www.0dtq.com/haining/15tian.html 推荐查看天气:https://www.0dtq.com/haining/40tian.html 推荐查看天气:https://www.0dtq.com/pinghu/ 推荐查看天气:https://www.0dtq.com/pinghu/zuotian.html 推荐查看天气:https://www.0dtq.com/pinghu/7tian.html 推荐查看天气:https://www.0dtq.com/pinghu/10tian.html 推荐查看天气:https://www.0dtq.com/pinghu/15tian.html 推荐查看天气:https://www.0dtq.com/pinghu/40tian.html 推荐查看天气:https://www.0dtq.com/tongxiang/ 推荐查看天气:https://www.0dtq.com/tongxiang/zuotian.html 推荐查看天气:https://www.0dtq.com/tongxiang/7tian.html 推荐查看天气:https://www.0dtq.com/tongxiang/10tian.html 推荐查看天气:https://www.0dtq.com/tongxiang/15tian.html 推荐查看天气:https://www.0dtq.com/tongxiang/40tian.html 推荐查看天气:https://www.0dtq.com/huzhou/ 推荐查看天气:https://www.0dtq.com/huzhou/zuotian.html 推荐查看天气:https://www.0dtq.com/huzhou/7tian.html 推荐查看天气:https://www.0dtq.com/huzhou/10tian.html 推荐查看天气:https://www.0dtq.com/huzhou/15tian.html 推荐查看天气:https://www.0dtq.com/huzhou/40tian.html 推荐查看天气:https://www.0dtq.com/wuxing/ 推荐查看天气:https://www.0dtq.com/wuxing/zuotian.html 推荐查看天气:https://www.0dtq.com/wuxing/7tian.html 推荐查看天气:https://www.0dtq.com/wuxing/10tian.html 推荐查看天气:https://www.0dtq.com/wuxing/15tian.html 推荐查看天气:https://www.0dtq.com/wuxing/40tian.html 推荐查看天气:https://www.0dtq.com/nanxun/ 推荐查看天气:https://www.0dtq.com/nanxun/zuotian.html 推荐查看天气:https://www.0dtq.com/nanxun/7tian.html 推荐查看天气:https://www.0dtq.com/nanxun/10tian.html 推荐查看天气:https://www.0dtq.com/nanxun/15tian.html 推荐查看天气:https://www.0dtq.com/nanxun/40tian.html 推荐查看天气:https://www.0dtq.com/deqingxian/ 推荐查看天气:https://www.0dtq.com/deqingxian/zuotian.html 推荐查看天气:https://www.0dtq.com/deqingxian/7tian.html 推荐查看天气:https://www.0dtq.com/deqingxian/10tian.html 推荐查看天气:https://www.0dtq.com/deqingxian/15tian.html 推荐查看天气:https://www.0dtq.com/deqingxian/40tian.html 推荐查看天气:https://www.0dtq.com/changxing/ 推荐查看天气:https://www.0dtq.com/changxing/zuotian.html 推荐查看天气:https://www.0dtq.com/changxing/7tian.html 推荐查看天气:https://www.0dtq.com/changxing/10tian.html 推荐查看天气:https://www.0dtq.com/changxing/15tian.html 推荐查看天气:https://www.0dtq.com/changxing/40tian.html 推荐查看天气:https://www.0dtq.com/anji/ 推荐查看天气:https://www.0dtq.com/anji/zuotian.html 推荐查看天气:https://www.0dtq.com/anji/7tian.html 推荐查看天气:https://www.0dtq.com/anji/10tian.html 推荐查看天气:https://www.0dtq.com/anji/15tian.html 推荐查看天气:https://www.0dtq.com/anji/40tian.html 推荐查看天气:https://www.0dtq.com/shaoxing/ 推荐查看天气:https://www.0dtq.com/shaoxing/zuotian.html 推荐查看天气:https://www.0dtq.com/shaoxing/7tian.html 推荐查看天气:https://www.0dtq.com/shaoxing/10tian.html 推荐查看天气:https://www.0dtq.com/shaoxing/15tian.html 推荐查看天气:https://www.0dtq.com/shaoxing/40tian.html 推荐查看天气:https://www.0dtq.com/yuecheng/ 推荐查看天气:https://www.0dtq.com/yuecheng/zuotian.html 推荐查看天气:https://www.0dtq.com/yuecheng/7tian.html 推荐查看天气:https://www.0dtq.com/yuecheng/10tian.html 推荐查看天气:https://www.0dtq.com/yuecheng/15tian.html 推荐查看天气:https://www.0dtq.com/yuecheng/40tian.html 推荐查看天气:https://www.0dtq.com/xinchang/ 推荐查看天气:https://www.0dtq.com/xinchang/zuotian.html 推荐查看天气:https://www.0dtq.com/xinchang/7tian.html 推荐查看天气:https://www.0dtq.com/xinchang/10tian.html 推荐查看天气:https://www.0dtq.com/xinchang/15tian.html 推荐查看天气:https://www.0dtq.com/xinchang/40tian.html 推荐查看天气:https://www.0dtq.com/zhuji/ 推荐查看天气:https://www.0dtq.com/zhuji/zuotian.html 推荐查看天气:https://www.0dtq.com/zhuji/7tian.html 推荐查看天气:https://www.0dtq.com/zhuji/10tian.html 推荐查看天气:https://www.0dtq.com/zhuji/15tian.html 推荐查看天气:https://www.0dtq.com/zhuji/40tian.html 推荐查看天气:https://www.0dtq.com/shangyu/ 推荐查看天气:https://www.0dtq.com/shangyu/zuotian.html 推荐查看天气:https://www.0dtq.com/shangyu/7tian.html 推荐查看天气:https://www.0dtq.com/shangyu/10tian.html 推荐查看天气:https://www.0dtq.com/shangyu/15tian.html 推荐查看天气:https://www.0dtq.com/shangyu/40tian.html 推荐查看天气:https://www.0dtq.com/chengzhou/ 推荐查看天气:https://www.0dtq.com/chengzhou/zuotian.html 推荐查看天气:https://www.0dtq.com/chengzhou/7tian.html 推荐查看天气:https://www.0dtq.com/chengzhou/10tian.html 推荐查看天气:https://www.0dtq.com/chengzhou/15tian.html 推荐查看天气:https://www.0dtq.com/chengzhou/40tian.html 推荐查看天气:https://www.0dtq.com/jinhua/ 推荐查看天气:https://www.0dtq.com/jinhua/zuotian.html 推荐查看天气:https://www.0dtq.com/jinhua/7tian.html 推荐查看天气:https://www.0dtq.com/jinhua/10tian.html 推荐查看天气:https://www.0dtq.com/jinhua/15tian.html 推荐查看天气:https://www.0dtq.com/jinhua/40tian.html 推荐查看天气:https://www.0dtq.com/jindong/ 推荐查看天气:https://www.0dtq.com/jindong/zuotian.html 推荐查看天气:https://www.0dtq.com/jindong/7tian.html 推荐查看天气:https://www.0dtq.com/jindong/10tian.html 推荐查看天气:https://www.0dtq.com/jindong/15tian.html 推荐查看天气:https://www.0dtq.com/jindong/40tian.html 推荐查看天气:https://www.0dtq.com/wuyi/ 推荐查看天气:https://www.0dtq.com/wuyi/zuotian.html 推荐查看天气:https://www.0dtq.com/wuyi/7tian.html 推荐查看天气:https://www.0dtq.com/wuyi/10tian.html 推荐查看天气:https://www.0dtq.com/wuyi/15tian.html 推荐查看天气:https://www.0dtq.com/wuyi/40tian.html 推荐查看天气:https://www.0dtq.com/jinhuapujiang/ 推荐查看天气:https://www.0dtq.com/jinhuapujiang/zuotian.html 推荐查看天气:https://www.0dtq.com/jinhuapujiang/7tian.html 推荐查看天气:https://www.0dtq.com/jinhuapujiang/10tian.html 推荐查看天气:https://www.0dtq.com/jinhuapujiang/15tian.html 推荐查看天气:https://www.0dtq.com/jinhuapujiang/40tian.html 推荐查看天气:https://www.0dtq.com/panan/ 推荐查看天气:https://www.0dtq.com/panan/zuotian.html 推荐查看天气:https://www.0dtq.com/panan/7tian.html 推荐查看天气:https://www.0dtq.com/panan/10tian.html 推荐查看天气:https://www.0dtq.com/panan/15tian.html 推荐查看天气:https://www.0dtq.com/panan/40tian.html 推荐查看天气:https://www.0dtq.com/lanxi/ 推荐查看天气:https://www.0dtq.com/lanxi/zuotian.html 推荐查看天气:https://www.0dtq.com/lanxi/7tian.html 推荐查看天气:https://www.0dtq.com/lanxi/10tian.html 推荐查看天气:https://www.0dtq.com/lanxi/15tian.html 推荐查看天气:https://www.0dtq.com/lanxi/40tian.html 推荐查看天气:https://www.0dtq.com/yiwu/ 推荐查看天气:https://www.0dtq.com/yiwu/zuotian.html

资源下载

更多资源
优质分享App

优质分享App

近一个月的开发和优化,本站点的第一个app全新上线。该app采用极致压缩,本体才4.36MB。系统里面做了大量数据访问、缓存优化。方便用户在手机上查看文章。后续会推出HarmonyOS的适配版本。

腾讯云软件源

腾讯云软件源

为解决软件依赖安装时官方源访问速度慢的问题,腾讯云为一些软件搭建了缓存服务。您可以通过使用腾讯云软件源站来提升依赖包的安装速度。为了方便用户自由搭建服务架构,目前腾讯云软件源站支持公网访问和内网访问。

Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

Sublime Text

Sublime Text

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。

用户登录
用户注册
修改密码
忘记密码