[Node.js]15. Level 3: Solving Backpressure
2023-09-14 08:59:22 时间
Even though we know pipe
does all the heavy lifting for us when dealing with backpressure, it's still a good idea for us to know about what is going on. Below, we are copying a file using Readable and Writeable streams.
Update the code to account for backpressure, without using pipe.
var fs = require('fs'); var file = fs.createReadStream("icon.png"); var newFile = fs.createWriteStream("icon-new.png"); file.on('data', function(chunk) { newFile.write(chunk); }); file.on('end', function() { newFile.end(); });
Answer:
var fs = require('fs'); var file = fs.createReadStream("icon.png"); var newFile = fs.createWriteStream("icon-new.png"); file.on('data', function(chunk) { var buffer = newFile.write(chunk); if(!buffer){ file.pause(); } }); newFile.on('drain', function(){ file.resume(); }); file.on('end', function() { newFile.end(); });
相关文章
- JavaWeb_Ajax通过JQuery和原生js异步传输数据
- JS自动缩放页面图片
- Node.js数据流Stream之Duplex流和Transform流
- Node.js之Console用法小结
- [Node.js] Add node.js command line to global
- [WASM] Run WebAssembly in Node.js using the node-loader
- [Whole Web, Node.js, PM2] Restarting your node.js app on code change using pm2
- [Whole Web, Node.js PM2] Loggin with PM2
- [Node.js] Using npm link to use node modules that are "in progress"
- [Node.js]32. Level 7: Working with Lists -- Redis
- [Node.js]26. Level 5 : Route rendering
- 01. WebApp2.0时代启程:Cocos2d-JS详解(一)
- [WASM] Run WebAssembly in Node.js using the node-loader
- [Server Running] [Node.js, PM2] Using PM2 To Keep Your Node Apps Alive
- [Node.js]15. Level 3: Solving Backpressure
- Node.js 的模块系统
- Atitit stomp.js conn连接activemq 目录 1.1. activemq 启动,已经默认开启了stomp ws的接口。。地址是1 1.2. Js 客户端代码1 1.3
- Atitit 前端测试最简化内嵌web服务器 php 与node.js 目录 1.1. php内置Web Server1 1.2. Node的2 Node的比较麻烦些。。Php更加简单
- vue.js学习:慕课网教程1:vue2.5入门
- js检测dom元素的变化
- 给你的站点加入 console.js
- Appium基础 — 补充:Node.js介绍
- 【JS高级】js之正则相关函数以及正则对象_02