Web APIs

MessagePort.close()

Try the free JavaScript compiler

Disentangle a port and stop communication through that connection. Close both endpoints when their lifecycle ends. Example console output: done.

Formal Syntax & Structure

javascript syntaxjavascript
port.close()

Return Value

undefined

No result value.

Working Example

Example Snippetjavascript
(async () => {const {port1,port2}=new MessageChannel(); const received=new Promise(resolve=>port2.onmessage=e=>resolve(e.data)); port1.postMessage("done"); console.log(await received); port1.close(); port2.close();})()
JavaScript Reference