From the course: JavaScript: Async
Unlock the full course today
Join today to access over 24,800 courses taught by industry experts.
Sending data to a web worker - JavaScript Tutorial
From the course: JavaScript: Async
Sending data to a web worker
- [Instructor] To use a Web Worker, you send it a message using the postMessage method. The method simply passes data, which can range from a simple string or number to a collection, like an array or object. Back in my filters.js file, I'm going to start by testing things out. So near the top, immediately after my worker variable, I'm going to send a message. And to do that, I reference that worker, that variable name that I created, and I use the postMessage method. And I'm just going to pass text, hello worker. And then after that, I'll add a console.log statement, just to say, message sent to worker. Just to verify that I've reached that point in my code. Now, after I send that message, I want to make sure that it actually got received. And to do that, I need to go to the code for the worker itself. And that's in filter-worker.JS. Within the code for this worker, I can use this to refer to the worker itself. And I…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.