使用setTimeout时,异步处理是什么?

时间:2018-12-22 02:48:04

标签: javascript node.js

通过串行连接时,立即发生“打开”事件,但是如果添加了setTimeout,它将在该时间之前延迟。我该如何解决?

const Serialport = require('serialport');

class Serial {
  connect() {
    const client = new Serialport('COM2', {
      autoOpen: false,
    });

    console.time('Complete Connect');
    client.open(err => {
      console.timeEnd('Complete Connect');
    });
  }
}

const serialport = new Serial();
serialport.connect();

setTimeout(() => {
  console.log('any work');
}, 3000);

打印:

1. any work
2. Complete Connect: 3025.978ms

0 个答案:

没有答案