node worker-threads 子线程日志打印顺序为什么不对???

代码如下
index.js

const { Worker } = require('worker_threads');
const path = require('path');

const worker = new Worker(path.join(__dirname,  './child.js'));

worker.on('message', (result) => {
  console.log(result);
});

worker.on('error', (err) => {
  console.log(err);
  console.log('出错误了');
})

worker.postMessage({'a': 'a'});

child.js

const { parentPort } = require('worker_threads');

const a = (m, n) => {
  return m + n;
}

parentPort.on('message', (msg) => {
  
  console.log(msg);
  const result = a(2, 3);
  console.log('aaaaa');

  parentPort.postMessage(result);
});

打印结果
image.png

为什么 aaaaa 在 5 之后打印, 谢谢

阅读 719
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏