From 3e672ddddd1d50bb550ad210cd4060dc3f472f52 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Thu, 14 Feb 2019 22:10:03 +0100 Subject: [PATCH 1/4] doc: improve worker_threads documentation This adds a few examples and clarifications. --- doc/api/worker_threads.md | 101 +++++++++++++++++++++++++++++++++++++- 1 file changed, 99 insertions(+), 2 deletions(-) diff --git a/doc/api/worker_threads.md b/doc/api/worker_threads.md index 840b831c903f6d..2b02a74f8f18b9 100644 --- a/doc/api/worker_threads.md +++ b/doc/api/worker_threads.md @@ -4,8 +4,8 @@ > Stability: 1 - Experimental -The `worker_threads` module enables the use of threads with message channels -between them. To access it: +The `worker_threads` module enables the use of threads that execute JS code +in parallel, with message channels between them. To access it: ```js const worker = require('worker_threads'); @@ -58,6 +58,18 @@ added: v10.5.0 Is `true` if this code is not running inside of a [`Worker`][] thread. +```js +const { Worker, isMainThread } = require('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'. +} +``` + ## worker.parentPort