账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    一个nodejs stream诡异的行为, 猜测和事件循环有关系, 求解释?
    47
    0

    当前目录下准备一个test.txt, 写入一些东西, 比如

    >It's for test<

    分别执行代码:

    1. 注释掉延时为 3 ms的代码块, 输出dest.txt, 内容为
    >It's for test<
    1. 注释掉延时为 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事件一次

    不同时间延迟, 输出不同, 行为十分诡异, 求解释?

    1
    打赏
    收藏
    点击回答
        全部回答
    • 0
    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部