generated at
WebWorkerの処理を待つ

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; }
これどうやってremoveEventListenerで解除すればいいんだろう?
とりあえず放置

使う時
js
const worker = new Worker('...'); const message = {...}; worker.onmessage = (e)=>{...} const promise = delegateWork({worker,message}); // いろんな処理 const result = await promise; // ここで結果が返ってくるのを待つ

Reference
元ネタ

#2021-02-08 00:38:30
#2021-01-15 15:56:18
#2020-12-10 04:52:42