阅读 42

Node.js 事件循环终极面试题

废话不多说,直接上代码:

function block(ms) {
  let start = Date.now();
  while (Date.now() - start < ms) {}
}
setTimeout(() => {
  console.log(1);
  setImmediate(() => {
    console.log(2);
  });
  new Promise((resolve) => {
    console.log(3);
    resolve();
  }).then(() => {
    console.log(4);
    process.nextTick(() => {
      console.log(5);
    });
  });
  process.nextTick(() => {
    console.log(6);
    Promise.resolve().then(() => {
      console.log(7);
    });
  });
});
setImmediate(() => {
  console.log(8);
});
setTimeout(() => {
  console.log(9);
  process.nextTick(() => {
    console.log(10);
  });
});
block(10);
复制代码

先po出来,过两天再写解析

文章分类
前端
文章标签