<p dir="auto">I've tried to create a simple example that utilizes workers to send and receive messages, but the main "thread" for lack of a better word fails to receive any kind of message from my worker.</p>
<p dir="auto">According to <code class="notranslate">deno -V</code> I'm using <code class="notranslate">deno 1.5.3</code></p>
<p dir="auto">Below are my test files, run them with <code class="notranslate">deno run --unstable --allow-read test.ts</code></p>
<details> 
  <summary>test.ts (click to expand)</summary>
<div class="highlight highlight-source-ts notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="const worker = new Worker(new URL('testworker.ts', import.meta.url).href, { type: 'module', deno: true });

worker.onmessage = (e) =&gt; {
    //This is never executed
    console.log('Stuff in worker done:', e.data);
};

worker.postMessage('test');"><pre class="notranslate"><span class="pl-k">const</span> <span class="pl-s1">worker</span> <span class="pl-c1">=</span> <span class="pl-k">new</span> <span class="pl-smi">Worker</span><span class="pl-kos">(</span><span class="pl-k">new</span> <span class="pl-smi">URL</span><span class="pl-kos">(</span><span class="pl-s">'testworker.ts'</span><span class="pl-kos">,</span> <span class="pl-k">import</span><span class="pl-kos">.</span><span class="pl-c1">meta</span><span class="pl-kos">.</span><span class="pl-c1">url</span><span class="pl-kos">)</span><span class="pl-kos">.</span><span class="pl-c1">href</span><span class="pl-kos">,</span> <span class="pl-kos">{</span> <span class="pl-c1">type</span>: <span class="pl-s">'module'</span><span class="pl-kos">,</span> <span class="pl-c1">deno</span>: <span class="pl-c1">true</span> <span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">;</span>

<span class="pl-s1">worker</span><span class="pl-kos">.</span><span class="pl-en">onmessage</span> <span class="pl-c1">=</span> <span class="pl-kos">(</span><span class="pl-s1">e</span><span class="pl-kos">)</span> <span class="pl-c1">=&gt;</span> <span class="pl-kos">{</span>
    <span class="pl-c">//This is never executed</span>
    <span class="pl-smi">console</span><span class="pl-kos">.</span><span class="pl-en">log</span><span class="pl-kos">(</span><span class="pl-s">'Stuff in worker done:'</span><span class="pl-kos">,</span> <span class="pl-s1">e</span><span class="pl-kos">.</span><span class="pl-c1">data</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
<span class="pl-kos">}</span><span class="pl-kos">;</span>

<span class="pl-s1">worker</span><span class="pl-kos">.</span><span class="pl-en">postMessage</span><span class="pl-kos">(</span><span class="pl-s">'test'</span><span class="pl-kos">)</span><span class="pl-kos">;</span></pre></div>
</details>
<details> 
  <summary>testworker.ts (click to expand)</summary>
<div class="highlight highlight-source-ts notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import Random from 'https://deno.land/x/random@v1.1.2/Random.js';

async function doStuff(data: any) {
    console.log('Doing stuff:', data);
    const r = new Random();
    const randInt = r.int(200, 2000);
    await new Promise((res, _) =&gt; setTimeout(() =&gt; res(randInt), 1000));
}

//deno-lint-ignore ban-ts-comment
//@ts-ignore
onmessage = async (e) =&gt; {
    try {
        //deno-lint-ignore ban-ts-comment
        //@ts-ignore
        postMessage(await doStuff(e.data))
        console.log('Stuff done:', e.data);
    } catch (e) {
        console.error('Stuff error', e);
    }
}"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-smi">Random</span> <span class="pl-k">from</span> <span class="pl-s">'https://deno.land/x/random@v1.1.2/Random.js'</span><span class="pl-kos">;</span>

<span class="pl-k">async</span> <span class="pl-k">function</span> <span class="pl-en">doStuff</span><span class="pl-kos">(</span><span class="pl-s1">data</span>: <span class="pl-smi">any</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
    <span class="pl-smi">console</span><span class="pl-kos">.</span><span class="pl-en">log</span><span class="pl-kos">(</span><span class="pl-s">'Doing stuff:'</span><span class="pl-kos">,</span> <span class="pl-s1">data</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
    <span class="pl-k">const</span> <span class="pl-s1">r</span> <span class="pl-c1">=</span> <span class="pl-k">new</span> <span class="pl-smi">Random</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
    <span class="pl-k">const</span> <span class="pl-s1">randInt</span> <span class="pl-c1">=</span> <span class="pl-s1">r</span><span class="pl-kos">.</span><span class="pl-en">int</span><span class="pl-kos">(</span><span class="pl-c1">200</span><span class="pl-kos">,</span> <span class="pl-c1">2000</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
    <span class="pl-k">await</span> <span class="pl-k">new</span> <span class="pl-smi">Promise</span><span class="pl-kos">(</span><span class="pl-kos">(</span><span class="pl-s1">res</span><span class="pl-kos">,</span> <span class="pl-s1">_</span><span class="pl-kos">)</span> <span class="pl-c1">=&gt;</span> <span class="pl-en">setTimeout</span><span class="pl-kos">(</span><span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-c1">=&gt;</span> <span class="pl-en">res</span><span class="pl-kos">(</span><span class="pl-s1">randInt</span><span class="pl-kos">)</span><span class="pl-kos">,</span> <span class="pl-c1">1000</span><span class="pl-kos">)</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
<span class="pl-kos">}</span>

<span class="pl-c">//deno-lint-ignore ban-ts-comment</span>
<span class="pl-c">//<span class="pl-k">@ts</span>-ignore</span>
<span class="pl-en">onmessage</span> <span class="pl-c1">=</span> <span class="pl-k">async</span> <span class="pl-kos">(</span><span class="pl-s1">e</span><span class="pl-kos">)</span> <span class="pl-c1">=&gt;</span> <span class="pl-kos">{</span>
    <span class="pl-k">try</span> <span class="pl-kos">{</span>
        <span class="pl-c">//deno-lint-ignore ban-ts-comment</span>
        <span class="pl-c">//<span class="pl-k">@ts</span>-ignore</span>
        <span class="pl-en">postMessage</span><span class="pl-kos">(</span><span class="pl-k">await</span> <span class="pl-en">doStuff</span><span class="pl-kos">(</span><span class="pl-s1">e</span><span class="pl-kos">.</span><span class="pl-c1">data</span><span class="pl-kos">)</span><span class="pl-kos">)</span>
        <span class="pl-smi">console</span><span class="pl-kos">.</span><span class="pl-en">log</span><span class="pl-kos">(</span><span class="pl-s">'Stuff done:'</span><span class="pl-kos">,</span> <span class="pl-s1">e</span><span class="pl-kos">.</span><span class="pl-c1">data</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
    <span class="pl-kos">}</span> <span class="pl-k">catch</span> <span class="pl-kos">(</span><span class="pl-s1">e</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
        <span class="pl-smi">console</span><span class="pl-kos">.</span><span class="pl-en">error</span><span class="pl-kos">(</span><span class="pl-s">'Stuff error'</span><span class="pl-kos">,</span> <span class="pl-s1">e</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
    <span class="pl-kos">}</span>
<span class="pl-kos">}</span></pre></div>
</details>
<p dir="auto">The output shows the following:</p>
<details> 
  <summary>Output (click to expand)</summary>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="Doing stuff: test
Stuff done: test"><pre class="notranslate"><code class="notranslate">Doing stuff: test
Stuff done: test
</code></pre></div>
</details>
<p dir="auto">As you can see, the line <code class="notranslate">Stuff in worker done:</code> is never logged to the console. Unless I'm doing something wrong that's just not how it's supposed to work as far as I'm aware.</p>
<p dir="auto">Example:</p>
<p dir="auto"><code class="notranslate">worker.ts</code>:</p>
<div class="highlight highlight-source-ts notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="self.addEventListener('message', ev =&gt; {
  try {
    const {data} = ev as MessageEvent;
    console.log('worker &lt;- main:', data);
    const value = data &gt; 0 ? data * 2 : undefined;
    (self as any).postMessage(value);
    console.log('worker -&gt; main:', value);
  }
  catch (ex) {
    console.error(ex);
  }
});"><pre class="notranslate"><span class="pl-s1">self</span><span class="pl-kos">.</span><span class="pl-en">addEventListener</span><span class="pl-kos">(</span><span class="pl-s">'message'</span><span class="pl-kos">,</span> <span class="pl-s1">ev</span> <span class="pl-c1">=&gt;</span> <span class="pl-kos">{</span>
  <span class="pl-k">try</span> <span class="pl-kos">{</span>
    <span class="pl-k">const</span> <span class="pl-kos">{</span>data<span class="pl-kos">}</span> <span class="pl-c1">=</span> <span class="pl-s1">ev</span> <span class="pl-k">as</span> <span class="pl-smi">MessageEvent</span><span class="pl-kos">;</span>
    <span class="pl-smi">console</span><span class="pl-kos">.</span><span class="pl-en">log</span><span class="pl-kos">(</span><span class="pl-s">'worker &lt;- main:'</span><span class="pl-kos">,</span> <span class="pl-s1">data</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
    <span class="pl-k">const</span> <span class="pl-s1">value</span> <span class="pl-c1">=</span> <span class="pl-s1">data</span> <span class="pl-c1">&gt;</span> <span class="pl-c1">0</span> ? <span class="pl-s1">data</span> <span class="pl-c1">*</span> <span class="pl-c1">2</span> : <span class="pl-c1">undefined</span><span class="pl-kos">;</span>
    <span class="pl-kos">(</span><span class="pl-s1">self</span> <span class="pl-k">as</span> <span class="pl-smi">any</span><span class="pl-kos">)</span><span class="pl-kos">.</span><span class="pl-en">postMessage</span><span class="pl-kos">(</span><span class="pl-s1">value</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
    <span class="pl-smi">console</span><span class="pl-kos">.</span><span class="pl-en">log</span><span class="pl-kos">(</span><span class="pl-s">'worker -&gt; main:'</span><span class="pl-kos">,</span> <span class="pl-s1">value</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
  <span class="pl-kos">}</span>
  <span class="pl-k">catch</span> <span class="pl-kos">(</span><span class="pl-s1">ex</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
    <span class="pl-smi">console</span><span class="pl-kos">.</span><span class="pl-en">error</span><span class="pl-kos">(</span><span class="pl-s1">ex</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
  <span class="pl-kos">}</span>
<span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">;</span></pre></div>
<p dir="auto"><code class="notranslate">main.ts</code>:</p>
<div class="highlight highlight-source-ts notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="const data = 0;

const main = async (): Promise&lt;void&gt; =&gt; {
  let resolve: (value: number) =&gt; void;

  const workerUrl = new URL('worker.ts', import.meta.url).href;
  const worker = new Worker(workerUrl, {deno: true, type: 'module'});

  const handleWorkerMessage = (ev: MessageEvent): void =&gt; {
    const {data} = ev;
    console.log('main &lt;- worker:', data);
    resolve(data);
    worker.terminate();
  };

  worker.addEventListener('message', handleWorkerMessage);
  worker.addEventListener('messageerror', () =&gt; console.log('message error'));
  worker.addEventListener('error', () =&gt; console.log('error'));

  const result = await new Promise(res =&gt; {
    resolve = res;
    worker.postMessage(data);
    console.log('main -&gt; worker:', data);
  });

  console.log(result);
};

if (import.meta.main) main();"><pre class="notranslate"><span class="pl-k">const</span> <span class="pl-s1">data</span> <span class="pl-c1">=</span> <span class="pl-c1">0</span><span class="pl-kos">;</span>

<span class="pl-k">const</span> <span class="pl-en">main</span> <span class="pl-c1">=</span> <span class="pl-k">async</span> <span class="pl-kos">(</span><span class="pl-kos">)</span>: <span class="pl-smi">Promise</span><span class="pl-kos">&lt;</span><span class="pl-smi"><span class="pl-k">void</span></span><span class="pl-kos">&gt;</span> <span class="pl-c1">=&gt;</span> <span class="pl-kos">{</span>
  <span class="pl-k">let</span> <span class="pl-s1">resolve</span>: <span class="pl-kos">(</span><span class="pl-s1">value</span>: <span class="pl-smi">number</span><span class="pl-kos">)</span> <span class="pl-c1">=&gt;</span> <span class="pl-smi"><span class="pl-k">void</span></span><span class="pl-kos">;</span>

  <span class="pl-k">const</span> <span class="pl-s1">workerUrl</span> <span class="pl-c1">=</span> <span class="pl-k">new</span> <span class="pl-smi">URL</span><span class="pl-kos">(</span><span class="pl-s">'worker.ts'</span><span class="pl-kos">,</span> <span class="pl-k">import</span><span class="pl-kos">.</span><span class="pl-c1">meta</span><span class="pl-kos">.</span><span class="pl-c1">url</span><span class="pl-kos">)</span><span class="pl-kos">.</span><span class="pl-c1">href</span><span class="pl-kos">;</span>
  <span class="pl-k">const</span> <span class="pl-s1">worker</span> <span class="pl-c1">=</span> <span class="pl-k">new</span> <span class="pl-smi">Worker</span><span class="pl-kos">(</span><span class="pl-s1">workerUrl</span><span class="pl-kos">,</span> <span class="pl-kos">{</span><span class="pl-c1">deno</span>: <span class="pl-c1">true</span><span class="pl-kos">,</span> <span class="pl-c1">type</span>: <span class="pl-s">'module'</span><span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">;</span>

  <span class="pl-k">const</span> <span class="pl-en">handleWorkerMessage</span> <span class="pl-c1">=</span> <span class="pl-kos">(</span><span class="pl-s1">ev</span>: <span class="pl-smi">MessageEvent</span><span class="pl-kos">)</span>: <span class="pl-smi"><span class="pl-k">void</span></span> <span class="pl-c1">=&gt;</span> <span class="pl-kos">{</span>
    <span class="pl-k">const</span> <span class="pl-kos">{</span>data<span class="pl-kos">}</span> <span class="pl-c1">=</span> <span class="pl-s1">ev</span><span class="pl-kos">;</span>
    <span class="pl-smi">console</span><span class="pl-kos">.</span><span class="pl-en">log</span><span class="pl-kos">(</span><span class="pl-s">'main &lt;- worker:'</span><span class="pl-kos">,</span> <span class="pl-s1">data</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
    <span class="pl-en">resolve</span><span class="pl-kos">(</span><span class="pl-s1">data</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
    <span class="pl-s1">worker</span><span class="pl-kos">.</span><span class="pl-en">terminate</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
  <span class="pl-kos">}</span><span class="pl-kos">;</span>

  <span class="pl-s1">worker</span><span class="pl-kos">.</span><span class="pl-en">addEventListener</span><span class="pl-kos">(</span><span class="pl-s">'message'</span><span class="pl-kos">,</span> <span class="pl-s1">handleWorkerMessage</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
  <span class="pl-s1">worker</span><span class="pl-kos">.</span><span class="pl-en">addEventListener</span><span class="pl-kos">(</span><span class="pl-s">'messageerror'</span><span class="pl-kos">,</span> <span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-c1">=&gt;</span> <span class="pl-smi">console</span><span class="pl-kos">.</span><span class="pl-en">log</span><span class="pl-kos">(</span><span class="pl-s">'message error'</span><span class="pl-kos">)</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
  <span class="pl-s1">worker</span><span class="pl-kos">.</span><span class="pl-en">addEventListener</span><span class="pl-kos">(</span><span class="pl-s">'error'</span><span class="pl-kos">,</span> <span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-c1">=&gt;</span> <span class="pl-smi">console</span><span class="pl-kos">.</span><span class="pl-en">log</span><span class="pl-kos">(</span><span class="pl-s">'error'</span><span class="pl-kos">)</span><span class="pl-kos">)</span><span class="pl-kos">;</span>

  <span class="pl-k">const</span> <span class="pl-s1">result</span> <span class="pl-c1">=</span> <span class="pl-k">await</span> <span class="pl-k">new</span> <span class="pl-smi">Promise</span><span class="pl-kos">(</span><span class="pl-s1">res</span> <span class="pl-c1">=&gt;</span> <span class="pl-kos">{</span>
    <span class="pl-s1">resolve</span> <span class="pl-c1">=</span> <span class="pl-s1">res</span><span class="pl-kos">;</span>
    <span class="pl-s1">worker</span><span class="pl-kos">.</span><span class="pl-en">postMessage</span><span class="pl-kos">(</span><span class="pl-s1">data</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
    <span class="pl-smi">console</span><span class="pl-kos">.</span><span class="pl-en">log</span><span class="pl-kos">(</span><span class="pl-s">'main -&gt; worker:'</span><span class="pl-kos">,</span> <span class="pl-s1">data</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
  <span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">;</span>

  <span class="pl-smi">console</span><span class="pl-kos">.</span><span class="pl-en">log</span><span class="pl-kos">(</span><span class="pl-s1">result</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
<span class="pl-kos">}</span><span class="pl-kos">;</span>

<span class="pl-k">if</span> <span class="pl-kos">(</span><span class="pl-k">import</span><span class="pl-kos">.</span><span class="pl-c1">meta</span><span class="pl-kos">.</span><span class="pl-c1">main</span><span class="pl-kos">)</span> <span class="pl-en">main</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">;</span></pre></div>
<p dir="auto">When run, it just hangs forever after the worker posts <code class="notranslate">undefined</code> back to the main process because the handler is never invoked:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="example % deno run --unstable --allow-read=. main.ts
main -&gt; worker: 0
worker &lt;- main: 0
worker -&gt; main: undefined
^C"><pre class="notranslate"><code class="notranslate">example % deno run --unstable --allow-read=. main.ts
main -&gt; worker: 0
worker &lt;- main: 0
worker -&gt; main: undefined
^C
</code></pre></div>
<p dir="auto">If—on line 1 of <code class="notranslate">main.ts</code>—<code class="notranslate">data</code> is changed to a number greater than <code class="notranslate">0</code>, the worker will post back a <code class="notranslate">number</code> instead of <code class="notranslate">undefined</code> and everything works as expected:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="example % deno run --unstable --allow-read=. main.ts
Check file:///Users/jesse/example/main.ts
main -&gt; worker: 1
worker &lt;- main: 1
worker -&gt; main: 2
main &lt;- worker: 2
2"><pre class="notranslate"><code class="notranslate">example % deno run --unstable --allow-read=. main.ts
Check file:///Users/jesse/example/main.ts
main -&gt; worker: 1
worker &lt;- main: 1
worker -&gt; main: 2
main &lt;- worker: 2
2
</code></pre></div>
<p dir="auto">I'm not sure why this behavior is happening, but <code class="notranslate">undefined</code> is a meaningful value, and this seems like a bug.</p>