0
点赞
收藏
分享

微信扫一扫

settimeout,setinternal, setImmediate,nexttick啥区别

color_小浣熊 2022-04-14 阅读 56
node.js

为什么settimeout 0 会比setimmediate晚执行,为什么nexttick会先执行,为什么存在nexttick,libuv是个啥,poll是个啥好好看看下面这篇就都懂了

简短的不让发,你让我发啥,我炒人家的又有什么意义,能用一句话解决的问题为什么需要让我打一堆的字来让这边文章发布??????

我粘几段代码演示一下吧

const fs = require('fs');

function someAsyncOperation(callback) {
  // Assume this takes 95ms to complete
  fs.readFile('/path/to/file', callback);
}

const timeoutScheduled = Date.now();

setTimeout(() => {
  const delay = Date.now() - timeoutScheduled;

  console.log(`${delay}ms have passed since I was scheduled`);
}, 100);

// do someAsyncOperation which takes 95 ms to complete
someAsyncOperation(() => {
  const startCallback = Date.now();

  // do something that will take 10ms...
  while (Date.now() - startCallback < 10) {
    // do nothing
  }
});
function apiCall(arg, callback) {
  if (typeof arg !== 'string')
    return process.nextTick(
      callback,
      new TypeError('argument should be string')
    );
}
const server = net.createServer();
server.on('connection', (conn) => {});

server.listen(8080);
server.on('listening', () => {});

The Node.js Event Loop, Timers, and process.nextTick() | Node.js (nodejs.org)

举报

相关推荐

0 条评论