jsasync function delegateWork({worker, message}) {
const job = new Promise((resolve, reject) =>
worker.addEventListener('message', message => {
resolve(message.data);
}, {once: true}));
worker.postMessage(message);
return await job;
}
jsconst worker = new Worker('...');
const message = {...};
worker.onmessage = (e)=>{...}
const promise = delegateWork({worker,message});
// いろんな処理
const result = await promise; // ここで結果が返ってくるのを待つ