Lesson 26 of 55
javascriptWeb Workers – Background Threading
9 mins
Course progress47%
What you'll learn
Keep these ideas in mind as you work through the lesson.
Lesson 26 of 55
javascript9 mins
Keep these ideas in mind as you work through the lesson.
Web Workers run in a separate thread with no direct DOM access.
// worker.js
self.addEventListener('message', e => {
const result = heavyCalc(e.data);
self.postMessage(result);
});
Main thread:
const worker = new Worker('worker.js');
worker.postMessage(42);
worker.onmessage = e => console.log('Result:', e.data);