`title: Streams 管道错误传播与恢复机制``categories: Web 开发/前端/数据管理``keywords: Streams,错误传播,pipeThrough,pipeTo,恢复``description: 解析 Streams 管道中的错误传播机制与关闭语义,设计重试与恢复策略,保障持久化与实时处理的稳定性。`错误处理在 `pipeThrough` 的 Transform 中捕获并选择性传播;`pipeTo` 返回的 promise 用于统一失败处理。示例const transform = new TransformStream({ transform(chunk, controller) { if (!chunk) throw new Error('invalid'); controller.enqueue(chunk); } }); async function process(src, writable) { try { await src.pipeThrough(transform).pipeTo(writable); } catch (e) { // 恢复策略:重新创建管道或回退到缓冲写入 } }

发表评论 取消回复