There is no way to modify a window.global
from a web-worker, as the web-worker has no reference to the window
object. You can communicate with the web-worker via window.postMessage
. Using this approach, you can pass the array back to the main thread, and set the global there:
Main Thread
var worker = new Worker('worker.js');
var globalArr;
worker.addEventListener('message', function (e) {
globalArr = e.data;
});
Worker Script
// do work
var arr = [1,3,5,]
self.postMessage(arr);