It's hard to coordinate other tasks with worker messages only using this API. For example, how would you return the result of a worker calculation from a function? At first, there's no obvious easy way to do so.
Thankfully, there's a clean way around this, which wraps message handlers in a Promise.
This idea uses closures to pass the resolve function from inside the Promise callback into the parent scope, and then into the web worker event handler. Now the worker can resolve the Promise itself without needing access to the inner scope of the Promise callback.
You can wrap the entire thing inside a function and return the Promise. Now, all you need to do to communicate with a web worker is call an async function!