chuankoog
V2EX  ›  Node.js

成为一名老 noder: 深入理解 Event Loop

  •  
  •   chuankoog · Mar 25, 2020 · 5157 views
    This topic created in 2261 days ago, the information mentioned may be changed or developed.

    身为一名老 noder, 我们都知道, nodejs 程序写得好不好, 就看对 eventloop 理解得到不到位.

    下面这段代码输出是多少呢?

    setTimeout(function () {
      setTimeout(function timeout () {
        console.log('1');
      },0);
    
      setImmediate(function immediate () {
        console.log('2');
      });
    }, 0);
    
    setImmediate(function () {
      setTimeout(function timeout () {
        console.log('3');
      },0);
    
      setImmediate(function immediate () {
        console.log('4');
      });
    });
    

    cliche

    setTimeout 与 setImmediate 之间的区别
    process.nextTick 与 setImmediate 之间的区别
    何时需要使用 process.nextTick
    promise 在 eventloop 中的定位
    

    单线程的 nodejs

    当我们在说单线程 nodejs 时, 其实是在说我们写的 nodejs 代码的执行是单线程的.

    实际上, nodejs 程序在运行时自己维护了一个线程池. 垃圾回收任务就是在单独的线程里执行的.

    more...

    No Comments Yet
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3089 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 15:00 · PVG 23:00 · LAX 08:00 · JFK 11:00
    ♥ Do have faith in what you're doing.