How to get child worker send message to parent in Deno with multiple workers?
I'm new on workers, so base on this question I was trying the following:
// main.t
for(let idx = 0; idx < 4; idx++){
const worker = new Worker(new URL('./worker.ts', import.meta.url).href, {
type: "module",
deno: { namespace: true},
});
worker.postMessage({ id: idx, name: 'text1', email: 'ea@da.com' });
worker.addEventListener('message', message => {
console.log('response', message.data)
})
}
// worker.ts
self.onmessage = async (params) => {
const data = params.data
console.log('params', data)
data.name = 'text2'
self.postMessage(data)
self.close()
}
Expecting this:
params { id: 2, name: "text1", email: "ea@da.com" }
response { id: 2, name: "text2", email: "ea@da.com" }
params { id: 0, name: "text1", email: "ea@da.com" }
response { id: 0, name: "text2", email: "ea@da.com" }
params { id: 1, name: "text1", email: "ea@da.com" }
response { id: 1, name: "text2", email: "ea@da.com" }
params { id: 3, name: "text1", email: "ea@da.com" }
response { id: 3, name: "text2", email: "ea@da.com" }
But instead what I'm getting is:
params { id: 0, name: "text1", email: "ea@da.com" }
params { id: 1, name: "text1", email: "ea@da.com" }
params { id: 2, name: "text1", email: "ea@da.com" }
params { id: 3, name: "text1", email: "ea@da.com" }
response { id: 3, name: "text2", email: "ea@da.com" }
Can someone help me with where I'm going wrong? Why I'm only getting responses from the last worker instead of getting a response from every worker?
from Recent Questions - Stack Overflow https://ift.tt/3iKDpG6
https://ift.tt/eA8V8J
Comments
Post a Comment