diff --git a/doc/api/worker_threads.md b/doc/api/worker_threads.md index 77afaaec9c5cc3..3c36a0a03cfdb9 100644 --- a/doc/api/worker_threads.md +++ b/doc/api/worker_threads.md @@ -125,6 +125,34 @@ if (isMainThread) { } ``` +## worker.receiveMessageOnPort(port) + + +* `port` {MessagePort} + +* Returns: {Object|undefined} + +Receive a single message from a given `MessagePort`. If no message is available, +`undefined` is returned, otherwise an object with a single `message` property +that contains the message payload, corresponding to the oldest message in the +`MessagePort`’s queue. + +```js +const { MessageChannel, receiveMessageOnPort } = require('worker_threads'); +const { port1, port2 } = new MessageChannel(); +port1.postMessage({ hello: 'world' }); + +console.log(receiveMessageOnPort(port2)); +// Prints: { message: { hello: 'world' } } +console.log(receiveMessageOnPort(port2)); +// Prints: undefined +``` + +When this function is used, no `'message'` event will be emitted and the +`onmessage` listener will not be invoked. + ## worker.SHARE_ENV