flâneur — a map of the web's best reading

Web Workers, comlink, TypeScript and React | johnnyreilly

johnnyreilly.com · 2,585 words · saved by 1 readers

JavaScript is famously single threaded. However, if you're developing for the web, you may well know that this is not quite accurate. There are Web Workers: A worker is an object created using a constructor (e.g. Worker()) that runs a named JavaScript file — this file contains the code that will run in the worker thread; workers run in another global context that is different from the current window. If you're using Vite to build your React app, you may prefer to read this post. Given that there is a way to use other threads for background processing, why doesn't this happen all the time? Well there's a number of reasons; not the least of which is the ceremony involved in interacting with Web Workers. Consider the following example that illustrates moving a calculation into a worker: This is not simple. It's hard to understand what's happening. Also, this approach only supports a single method call. I'd much rather write something that looked more like this: There's a way to do this us

JavaScript is famously single threaded. However, if you're developing for the web, you may well know that this is not quite accurate. There are Web Workers : A worker is an object created using a constructor (e.g. Worker() ) that runs a named JavaScript file — this file contains the code that will run in the worker thread; workers run in another global context that is different from the current window. If you're using Vite to build your React app, you may prefer to read this post . Given that there is a way to use other threads for background processing, why doesn't this happen all the time? W

Explore this link on the map →

saved by

related reading