aboutsummaryrefslogtreecommitdiff
path: root/docs/api
diff options
context:
space:
mode:
authorGravatar Colin McDonnell <colinmcd94@gmail.com> 2023-09-19 16:31:38 -0700
committerGravatar GitHub <noreply@github.com> 2023-09-19 16:31:38 -0700
commit4fce34854be8e1f29bf89f02f7a2541ef5e1d3a8 (patch)
tree48873680c39417eb97a62a341a6cf217318efd40 /docs/api
parent615beee1ae0fd9b299fc38ac0989d4be5899c19b (diff)
downloadbun-4fce34854be8e1f29bf89f02f7a2541ef5e1d3a8.tar.gz
bun-4fce34854be8e1f29bf89f02f7a2541ef5e1d3a8.tar.zst
bun-4fce34854be8e1f29bf89f02f7a2541ef5e1d3a8.zip
Doc updates (#5759)
* WIP * WIP
Diffstat (limited to 'docs/api')
-rw-r--r--docs/api/workers.md15
1 files changed, 12 insertions, 3 deletions
diff --git a/docs/api/workers.md b/docs/api/workers.md
index 50fdb9c3d..2b8c4fe13 100644
--- a/docs/api/workers.md
+++ b/docs/api/workers.md
@@ -10,7 +10,7 @@ Bun implements a minimal version of the [Web Workers API](https://developer.mozi
Like in browsers, [`Worker`](https://developer.mozilla.org/en-US/docs/Web/API/Worker) is a global. Use it to create a new worker thread.
-From the main thread:
+### From the main thread
```js#Main_thread
const workerURL = new URL("worker.ts", import.meta.url).href;
@@ -22,16 +22,25 @@ worker.onmessage = event => {
};
```
-Worker thread:
+### Worker thread
```ts#worker.ts_(Worker_thread)
+// prevents TS errors
+declare var self: Worker;
+
self.onmessage = (event: MessageEvent) => {
console.log(event.data);
postMessage("world");
};
```
-You can use `import`/`export` syntax in your worker code. Unlike in browsers, there's no need to specify `{type: "module"}` to use ES Modules.
+To prevent TypeScript errors when using `self`, add this line to the top of your worker file.
+
+```ts
+declare var self: Worker;
+```
+
+You can use `import` and `export` syntax in your worker code. Unlike in browsers, there's no need to specify `{type: "module"}` to use ES Modules.
To simplify error handling, the initial script to load is resolved at the time `new Worker(url)` is called.