當前目錄下準備一個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事件一次不同時間延遲, 輸出不同, 行為十分詭異, 求解釋?
一個nodejs stream詭異的行為, 猜測和事件循環有關系, 求解釋?
慕田峪4524236
2019-03-19 18:44:41