worker_threads.isMainThread


新增于: v10.5.0

如果此代码不在 Worker 线程内运行,则为 true

\Is true if this code is not running inside of a Worker thread.

import { Worker, isMainThread } from 'node:worker_threads';
if (isMainThread) {
 // This re-loads the current file inside a Worker instance.
 new Worker(new URL(import.meta.url));
} else {
 console.log('Inside Worker!');
 console.log(isMainThread); // Prints 'false'.
}'use strict';
const { Worker, isMainThread } = require('node:worker_threads');
if (isMainThread) {
 // This re-loads the current file inside a Worker instance.
 new Worker(__filename);
} else {
 console.log('Inside Worker!');
 console.log(isMainThread); // Prints 'false'.
}

AltStyle によって変換されたページ (->オリジナル) /