WebWorkerの処理を待つ
code:js
async 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;
}
とりあえず放置
使う時
code:js
const worker = new Worker('...');
const message = {...};
worker.onmessage = (e)=>{...}
const promise = delegateWork({worker,message});
// いろんな処理
const result = await promise; // ここで結果が返ってくるのを待つ
Reference
元ネタ