当前目录下准备一个test.txt, 写入一些东西, 比如
>It's for test<
分别执行代码:
注释掉延时为 3 ms的代码块, 输出dest.txt, 内容为
>It's for test<
注释掉延时为 2 ms的代码块, 输出dest.txt, 内容为
>It's for test<>It's for test<
以下为代码
const fs = require('fs')const from = fs.createReadStream('test.txt')const to = fs.createWriteStream('dest.txt', {
flags: 'a'})from.pipe(to, {
end: false})from.on('end', () => { console.log('end')
})// setTimeout(() => {// from.pipe(to)//this won't work, if time >= 3// }, 3)// setTimeout(() => {// from.pipe(to)//this will work, if time < 3// }, 2)只触发end事件一次
不同时间延迟, 输出不同, 行为十分诡异, 求解释?
慕田峪4524236
斯蒂芬大帝
红糖糍粑
随时随地看视频慕课网APP